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));
这里我得到一个例外:
The specified type member 'BaseUser' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
有什么解决办法吗?
您已经编写了 Linq 查询。您正在与 Entity Framework 合作。所以,你正在使用 Linq-To-EntityFramework.
.Net
将根据您使用的 RDMS 类型将您的 Linq
查询转换为数据库查询。
例如,让我们看这段代码:
context.Users.Select(x => x.Id == 5);
这将被翻译成:
select * from User where Id=5;
因此,这意味着如果 .net 无法将其转换为数据库查询,则会抛出异常。例如,您的查询。您已经在 class 中创建了 属性。你相信它会被翻译成数据库查询吗?如何?没有办法!这就是异常的原因。
此外,您的 BaseUser
属性 对我来说似乎很不寻常。 BaseUser
将与 User
相同,如果您已正确配置所有内容。
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));
这里我得到一个例外:
The specified type member 'BaseUser' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
有什么解决办法吗?
您已经编写了 Linq 查询。您正在与 Entity Framework 合作。所以,你正在使用 Linq-To-EntityFramework.
.Net
将根据您使用的 RDMS 类型将您的 Linq
查询转换为数据库查询。
例如,让我们看这段代码:
context.Users.Select(x => x.Id == 5);
这将被翻译成:
select * from User where Id=5;
因此,这意味着如果 .net 无法将其转换为数据库查询,则会抛出异常。例如,您的查询。您已经在 class 中创建了 属性。你相信它会被翻译成数据库查询吗?如何?没有办法!这就是异常的原因。
此外,您的 BaseUser
属性 对我来说似乎很不寻常。 BaseUser
将与 User
相同,如果您已正确配置所有内容。