9 changed files with 299 additions and 50 deletions
@ -0,0 +1,70 @@ |
|||||
|
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 |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue