Canonical Function “EntityFunctions.TruncateTime” does not exist in MYSQL

前端 未结 2 690
遥遥无期
遥遥无期 2020-12-06 03:27

I\'m trying to run this query:

DateTime DDate=DateTime.Today; //Today\'s date without Time
var v= db.measurements.Where(m => EntityFunctions.TruncateTime(         


        
2条回答
  •  粉色の甜心
    2020-12-06 04:02

    The approach in A Torres' response works, but it felt a bit hacky for me, so I have found another approach (This works for EF6, for EF5 a similar approach exists, but I can't test it):

    1. Create class DatabaseFunctions and add the following code to it:

      [Function(FunctionType.BuiltInFunction, "Date")]
      public static DateTime? Date(DateTime? dateValue) 
          => Function.CallNotSupported();
      
    2. Add the following line to OnModelCreating in your DbContext

      protected override void OnModelCreating(DbModelBuilder modelBuilder){
          modelBuilder.Conventions.Add(new FunctionConvention())
          // ...
      }
      
    3. Use DatabaseFunctions.Date instead of EntityFunctions.TruncateTime.

提交回复
热议问题