zoukankan      html  css  js  c++  java
  • Entity Framework 中的Code First 中引入数据库函数

    1,在项目中添加CodeFirstStoreFunctions包:

    Install-Package EntityFramework.CodeFirstStoreFunctions

    2,注册注册函数,FunctionsConvention第二个参数为函数定义所在的类

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    { 
    base.OnModelCreating(modelBuilder); 
    //注册函数 
    modelBuilder.Conventions.Add(new FunctionsConvention("dbo", this.GetType())); 
    } 

    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) }
  • 相关阅读:
    web前端优化之reflow(减少页面的回流)
    Javascript深拷贝
    MySQL 配置优化
    MySQ中Lmax_connections的合理设置
    Too many connections解决方案
    Linux 查看文件内容
    ON DUPLICATE KEY UPDATE
    jquery $.each 和for怎么跳出循环终止本次循环
    使用redis避免客户端频繁提交数据
    windows下为mysql添加日志
  • 原文地址:https://www.cnblogs.com/ejiyuan/p/1720937.html
Copyright © 2011-2022 走看看