实体框架6代码第一个自定义函数 [英] Entity Framework 6 Code First Custom Functions

查看:106
本文介绍了实体框架6代码第一个自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试类似的内容:



如何使用带有LINQ的实体的标量值函数?



但是我没有使用EDMX,而只是DbContext和代码。



我遇到过:



< a href =https://codefirstfunctions.codeplex.com/ =nofollow noreferrer> https://codefirstfunctions.codeplex.com/



  var locations = context.Locations.Where(e => ; Functions.LatLongDistanceCalc(e.Lat,e.Long,lat,long)> = 10)

它将在SQL Server上调用标量函数(LatLongDistanceCalc)。



有没有什么办法可以不使用EDMX?我知道你可以构造一个手动查询,但是这不会是优先考虑的,因为我希望使用延迟加载代理等恢复实体,以及构建一个更复杂的查询。

解决方案

您应该可以在 Where 标准中使用标量SQL函数, CodeFirstStoreFunctions



假设您要映射SQL函数[dbo]。[LatLongDistanceCalc]和根据测试套件

  public class MyDataContext:DbContext 
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...

modelBuilder.Conventions.Add(new FunctionsConvention(dbo,this.GetType()));
}

//CodeFirstDatabaseSchema是一个约定强制模式名称
//LatLongDistanceCalc是您的函数名称

[DbFunction CodeFirstDatabaseSchema,LatLongDistanceCalc)]
public static int LatLongDistanceCalc(int fromLat,int fromLong,
int toLat,int toLong)
{
//不需要提供实现
抛出新的NotSupportedException();
}
}

的使用将是:

  context.Locations 
.Where(e => MyDataContext.LatLongDistanceCalc(e.Lat,e.Long,lat,long)> = 10)


I'm trying something similar to this:

How to use scalar-valued function with linq to entity?

However I'm not using EDMX, but instead just DbContext and code first.

I've come across this:

https://codefirstfunctions.codeplex.com/

But the usage isn't suitable. What I am trying to achieve is to be able to do this:

var locations = context.Locations.Where(e => Functions.LatLongDistanceCalc(e.Lat, e.Long, lat, long) >= 10)

Where it will call a scalar function (LatLongDistanceCalc) on SQL Server.

Is there any way to do this without using EDMX? I know that you can construct a manual query but this wouldn't be prefereable because I want to bring back entities with lazy loading proxies etc as well as building up a more complex query.

解决方案

You should be able to use a scalar SQL function in your Where criterias with CodeFirstStoreFunctions

Assuming you want to map SQL function [dbo].[LatLongDistanceCalc], and according to the test suite:

public class MyDataContext: DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       //...

       modelBuilder.Conventions.Add(new FunctionsConvention("dbo", this.GetType()));
    }

    // "CodeFirstDatabaseSchema" is a convention mandatory schema name
    // "LatLongDistanceCalc" is the name of your function

    [DbFunction("CodeFirstDatabaseSchema", "LatLongDistanceCalc")]
    public static int LatLongDistanceCalc(int fromLat, int fromLong,
                                                       int toLat, int toLong)
    {
       // no need to provide an implementation
       throw new NotSupportedException();
    }
}

usage would then be:

context.Locations
       .Where(e => MyDataContext.LatLongDistanceCalc(e.Lat, e.Long, lat, long) >= 10)

这篇关于实体框架6代码第一个自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆