如何防止 EF 尝试在我的模型上映射方法?
因此,我有一个具有普通属性的“Speaker”类,以及一个像这样的普通旧方法:
public string FullNameFirstLast()
{
return FirstName + " " + LastName;
}
我正在使用 Entity Framework Code First Magic Unicorn 版本,并且收到此错误:
System.NotSupportedException:LINQ to 实体无法识别该方法 'System.String FullNameFirstLast()' 方法,并且该方法不能 翻译成商店表达式。
这看起来很奇怪,因为它是一个方法而不是一个属性,所以我想知道为什么 EF 无论如何都会关心它...所以我尝试告诉 EF 像这样忽略它:
modelBuilder.Entity<Speaker>()
.Ignore(ignore => ignore.FullNameFirstLast())
;
这没有帮助,因为现在我得到了这个错误消息代替:
System.InvalidOperationException: 表达式'忽略=> ignore.FullNameFirstLast()' 不是 有效的属性表达式。这 表达式应该代表一个 属性:C#: 't => t.我的财产' VB.Net:“函数(t)t.MyProperty”。
我该怎么做?我假设我可以使用 EF 在我的模型对象上拥有方法,对吗?
So, I've got a "Speaker" class, with normal properties, and a plain old method like this:
public string FullNameFirstLast()
{
return FirstName + " " + LastName;
}
I'm using Entity Framework Code First Magic Unicorn edition, and I get this error:
System.NotSupportedException: LINQ to
Entities does not recognize the method
'System.String FullNameFirstLast()'
method, and this method cannot be
translated into a store expression.
Which seems odd, since it is a method and not a property, so I wonder why EF would care about it anyway... So I tried telling EF to ignore it like so:
modelBuilder.Entity<Speaker>()
.Ignore(ignore => ignore.FullNameFirstLast())
;
Which doesn't help, because now I get this error message instead:
System.InvalidOperationException: The
expression 'ignore =>
ignore.FullNameFirstLast()' is not a
valid property expression. The
expression should represent a
property: C#: 't => t.MyProperty'
VB.Net: 'Function(t) t.MyProperty'.
How do I do this? I'm assuming that I can have methods on my model objects with EF, right??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是映射的问题。这是在 Linq-to-entities 查询中使用该方法的问题。您不能在 Linq-to-entities 查询中使用自定义方法或自定义属性,因为 EF 提供程序不知道如何将它们转换为 SQL。
It is not a problem of mapping. It is problem of using the method in Linq-to-entities query. You can't use custom methods or custom properties in Linq-to-entities query because EF provider doesn't know how to translate them to SQL.