how to prevent EF from trying to map a method on my model?
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 i开发者_JS百科t 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??
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.
精彩评论