时间:2021-07-01 10:21:17 帮助过:2人阅读
3,定义函数对应的方法,要入数据中函数签名一致
[DbFunctionAttribute("CodeFirstDatabaseSchema", "getDistance")] public static double get_distance(double curLat, double curLng, double srcLat, double srcLng) { throw new NotSupportedException(); }
4,创建数据库函数
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 DOUBLE; set dis = 6370996.81 * ACOS( COS(srcLat * PI() / 180) * COS(curLat * PI() / 180) * COS( srcLng * PI() / 180 - curLng * PI() / 180 ) + SIN(srcLat * PI() / 180) * SIN(curLat * PI() / 180)); RETURN dis; END"); }
5,调用
from moduleInfo in _shopInfoRepository.Entities select new ShopInfoBll() { PositionX = moduleInfo.PositionX,
PositionY = moduleInfo.PositionX,
Distance= EFDbContextMobile.get_distance(40.09914694048, 116.42105702279, moduleInfo.PositionY, moduleInfo.PositionX) }
Entity Framework 中的Code First 中引入数据库函数
标签: