EF architecture ~ Let mysql support DbFunctions extension functions, mysqldbfunctions

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.