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
{
///
///
///
public class ESUNAMESContext : DbContext
{
///
///
///
///
public ESUNAMESContext(DbContextOptions options)
: base(options)
{
//Configuration.ProxyCreationEnabled = false;
}
///
///
///
///
///
///
public int? DatePart(string datePartArg, DateTimeOffset? date) => throw new InvalidOperationException($"{nameof(DatePart)} cannot be called client side.");
///
///
///
///
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
)
);
}
}
}