我想将Entity Framework 6集成到我们的系统中,但是有问题.
问题是:
我不能通过像[Table],[Column]这样的属性来映射函数.只有1个属性可用[DbFunction],它需要*.edmx文件.
我可以在*.edmx文件中映射函数,但这意味着我不能使用实体的属性映射:[Table],[Column].必须在*.edmx或属性中填充映射.
我尝试通过以下代码创建DbModel并添加函数:
public static class Functions
{
[DbFunction("CodeFirstNamespace", "TestEntity")]
public static string TestEntity()
{
throw new NotSupportedException();
}
}
public class MyContext : DbContext, IDataAccess
{
protected MyContext (string connectionString)
: base(connectionString, CreateModel())
{
}
private static DbCompiledModel CreateModel()
{
var dbModelBuilder = new DbModelBuilder(DbModelBuilderVersion.Latest);
dbModelBuilder.Entity<Warehouse>();
var dbModel = dbModelBuilder.Build(new DbProviderInfo("System.Data.SqlClient", "2008"));
var edmType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String);
var payload =
new EdmFunctionPayload
{
Schema = "dbo",
ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion, …Run Code Online (Sandbox Code Playgroud) c# entity-framework sql-function ef-code-first entity-framework-6
我有一个像这样的表达式:
var values = Enumerable.Range(1,2);
return message => message.Properties.Any(
p => p.Key == name
&& int.Parse(p.Value) >= values[0]
&& int.Parse(p.Value) <= values[1]);
Run Code Online (Sandbox Code Playgroud)
这编译得很好但是当它命中数据库时会抛出异常 'LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression '
如果我不进行解析并且有值,那么string[]我就不能在字符串上使用>=和<=运算符.
p.Value 是一个包含各种值的字符串,但在这种情况下它是 int
有没有办法可以查询数据库来执行这种语句之间的操作?
我正在尝试类似的东西:
但是我没有使用EDMX,而是先使用DbContext和代码.
我遇到过这个:
https://codefirstfunctions.codeplex.com/
但用法不合适.我想要实现的是能够做到这一点:
var locations = context.Locations.Where(e => Functions.LatLongDistanceCalc(e.Lat, e.Long, lat, long) >= 10)
Run Code Online (Sandbox Code Playgroud)
它将在SQL Server上调用标量函数(LatLongDistanceCalc).
没有使用EDMX有没有办法做到这一点?我知道你可以构建一个手动查询,但这不是优先考虑的,因为我想带回具有延迟加载代理等的实体以及构建更复杂的查询.