1. Add the Codefirststorefunctions package to the project:
Install-package entityframework.codefirststorefunctions
2, register function, functionsconvention the second parameter is the class where the function definition resides
protected Override void base//modelBuilder.Conventions.Add (new Functionsconvention ("dbo" This
3, define the function corresponding to the method, to enter the data in the function signature is consistent
[Dbfunctionattribute ("codefirstdatabaseschema""getdistance" public static double Get_distance (double double Double Double srclng) { thrownew NotSupportedException ();}
4, creating a database function
protected Override void Seed (Efdbcontextmobile context) { context. Database.executesqlcommand (@ "DROP FUNCTION IF EXISTS getdistance; CREATE FUNCTION getdistance (Curlat double, curlng double, Srclat double, srclng double) RETURNS double BEGIN DECLARE dis D ouble; Set dis = 6370996.81 * ACOS (cos (srclat * PI ()/+/-) * cos (Curlat * PI ()/) * cos (SRCLNG * PI ()/180-curlng * PI ( ) + sin (Srclat * pi ()/curlat) * SIN (* * PI ()/180)); RETURN dis; END");}
5, call
from inch Select New = Moduleinfo.positionx,
Positiony = Moduleinfo.positionx,
Distance= efdbcontextmobile.get_distance (40.09914694048116.42105702279, Moduleinfo.positiony, Moduleinfo.positionx)}
Introduction of database functions in code first in the Entity Framework