EF architecture ~ Let mysql support DbFunctions extension functions, mysqldbfunctions
Back to directory
For the use of date functions in Linq To Entity, the extension method in DbFunctions is required, instead of the extension method. net, because the linq code will be translated into SQL and sent to the database, such as your. the net method is unknown to the database, so you need to use the function in DbFunctions. It is designed for sqlserver. If your data source is mysql, you will be embarrassed, I started to think Mysql. these extended functions are integrated in Data. Unfortunately, they are not integrated, so we need to use other solutions.
Database functions in DbFunctions
MySql-supported solutions
Add an object's self-function in mysql
BEGIN # returns the number of months with different dates RETURN PERIOD_DIFF (DATE_FORMAT ('to', '% Y % m'), DATE_FORMAT ('from',' % Y % m ')); END
Then you can use DbFunctions in linq to get OK, so there will be no problem with undefined functions.
Var result = repository. GetModel (I => DbFunctions. DiffMonths (I. CreateDate, DateTime. Now)> 1)
. Take (20)
. ToList ();
Then the program can be used normally. Thank you for reading this article!
Back to directory