LINQ to Entities不支持指定的类型成员。实体成员和实体导航 [英] The specified type member is not supported in LINQ to Entities. entity members, and entity navigation
问题描述
public partial class User : IUser
{
public long ID {get; set;}
public BaseUser BaseUser
{
get
{
var context = new Factory().Create<ContextDB>();
return context.Users.Find(this.ID);
}
}
}
和
var result = _Context.Employees.Where(t => t.User.BaseUser.UserName.ToLower().Trim().Contains(searchKey));
这里我有一个例外:
LINQ to Entities不支持指定的类型成员BaseUser。
The specified type member 'BaseUser' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
任何解决方案?
推荐答案
您已经写了 Linq 查询。而您正在使用 实体框架 。因此,您正在使用 Linq-To-EntityFramework 。
.Net
将会翻译根据您正在使用的RDMS的类型,您的 Linq
查询到数据库查询。
例如,我们来看这个代码:
.Net
will translate your Linq
query to database query based on the type of the RDMS which you are using.
For example, let's take this code:
context.Users.Select(x => x.Id == 5);
这将被翻译成:
select * from User where Id=5;
所以,这意味着.net会抛出异常,如果它无法将其转换为数据库查询。例如,您的查询。您已经在课堂上创建了属性。而且,您是否会将其翻译成数据库查询?怎么样?不可能!这是例外的原因。
So, it means that .net will throw exception if it couldn't translate it to the database query. For example, your query. You have created property in your class. And you are beleiving that it will be translated to the database query? How? There is no way! This is the reason of the exception.
此外,您的 BaseUser
属性对我来说似乎不寻常。 BaseUser
将与 User
相同,如果您已经配置了所有权利。
Also, your BaseUser
property seems unusual to me. BaseUser
will be same with User
if you have configured everything right.
这篇关于LINQ to Entities不支持指定的类型成员。实体成员和实体导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!