You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.2 KiB
70 lines
2.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AMESCoreStudio.WebApi;
|
|
using AMESCoreStudio.WebApi.Models.SYS;
|
|
using AMESCoreStudio.WebApi.Models.AMES;
|
|
using AMESCoreStudio.WebApi.Models.BAS;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
|
|
using System.Reflection;
|
|
|
|
|
|
namespace AMESCoreStudio.WebApi
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ESUNAMESContext : DbContext
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="options"></param>
|
|
public ESUNAMESContext(DbContextOptions<ESUNAMESContext> options)
|
|
: base(options)
|
|
{
|
|
//Configuration.ProxyCreationEnabled = false;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="datePartArg"></param>
|
|
/// <param name="date"></param>
|
|
/// <returns></returns>
|
|
public int? DatePart(string datePartArg, DateTimeOffset? date) => throw new InvalidOperationException($"{nameof(DatePart)} cannot be called client side.");
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="modelBuilder"></param>
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
var methodInfo = typeof(DbFunctionExtensions).GetMethod(nameof(DatePart));
|
|
|
|
var datePartMethodInfo = typeof(ESUNAMESContext)
|
|
.GetRuntimeMethod(nameof(ESUNAMESContext.DatePart), new[] { typeof(string), typeof(DateTimeOffset) });
|
|
modelBuilder.HasDbFunction(datePartMethodInfo)
|
|
.HasTranslation(args =>
|
|
new SqlFunctionExpression("DatePart",
|
|
new[]
|
|
{
|
|
new SqlFragmentExpression((args.ToArray()[0] as SqlConstantExpression).Value.ToString()),
|
|
args.ToArray()[1]
|
|
},
|
|
true,
|
|
new[] { false, false },
|
|
typeof(int?),
|
|
null
|
|
)
|
|
);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|