为什么 EF 核心告诉我 'specified member is unmapped'?
Why EF core tell me 'specified member is unmapped'?
我想通过使用投影查询(通过 Select)按名称和 return 新 DTO 过滤用户:
var result = context.Users
.Where(user => user.FullName == search)
.Select(u => new UserPagingViewModel
{
Id = u.Id,
IsSearchable = u.IsSearchable,
FirstName = u.FirstName,
LastName = u.LastName,
FullName = u.FullName,
Photo200 = u.Photo200,
City = u.City,
About = u.About,
Skills = u.UserSkills.Select(us => us.Skill.Name),
IsTeamMember = u.UserTeams.Any(ut => ut.UserAction == UserActionEnum.JoinedTeam)
})
.ToList();
用户class:
public class User : IHasId
{
public long Id { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string About { get; set; }
public string Telegram { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public string FullName => FirstName + " " + SecondName + " " + LastName;
public string Photo100 { get; set; }
public string Photo200 { get; set; }
public bool IsModerator { get; set; }
public List<UserTeam> UserTeams { get; set; }
public List<UserSkill> UserSkills { get; set; }
[NotMapped]
public List<Team> TeamsToRecruit { get; set; }
[NotMapped]
public bool AnyTeamOwner { get; set; }
}
数据库是 PostgreSql
。数据提供者 PostgreSQL/Npgsql provider for Entity Framework Core
但是当我尝试执行此请求时,出现异常并显示以下消息:
The LINQ expression 'DbSet<User>()
.Where(u => user.FullName == search)' could not be translated. Additional information: Translation of member 'FullName' on entity type 'User'
failed. This commonly occurs when the specified member is unmapped.
Either rewrite the query in a form that can be translated, or switch
to client evaluation explicitly by inserting a call to 'AsEnumerable',
'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See
https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我不想使用客户评价。
例如,如果我比较 FirstName 属性 一切正常。
请帮我想办法解决这个问题。
您在三个地方有全名。由于您没有全名列(或计算列),因此不能在 dbcontext 查询中使用它。它将生成如下所示的 sql。
SELECT FULLNAME,... FROM USERS WHERE FULLNAME = N"some value".
这就是您收到错误的原因。
您需要从 dbcontext 查询中删除所有 FullName 并像下面那样执行 where 过滤器。
var result = context.Users
.Where(x => (x.FirstName + " " + x.SecondName + " " + x.LastName) == search)
.Select(u => new UserPagingViewModel
{
Id = u.Id,
IsSearchable = u.IsSearchable,
FirstName = u.FirstName,
LastName = u.LastName,
//FullName = u.FullName,
Photo200 = u.Photo200,
City = u.City,
About = u.About,
Skills = u.UserSkills.Select(us => us.Skill.Name),
IsTeamMember = u.UserTeams.Any(ut => ut.UserAction == UserActionEnum.JoinedTeam)
})
.ToList();
你会像下面这样
FROM [Users] AS [t]
WHERE ([t].[FirstName] + N' ' + [t].[SecondName] + N' ' + [t].[LastName]) = N'some value'
然后从用户对象中删除 FullName。
public class User : IHasId
{
public long Id { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string About { get; set; }
public string Telegram { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
// public string FullName => FirstName + " " + SecondName + " " + LastName;
public string Photo100 { get; set; }
public string Photo200 { get; set; }
public bool IsModerator { get; set; }
public List<UserTeam> UserTeams { get; set; }
public List<UserSkill> UserSkills { get; set; }
[NotMapped]
public List<Team> TeamsToRecruit { get; set; }
[NotMapped]
public bool AnyTeamOwner { get; set; }
}
将全名添加到 UserPagingViewModel。
public class UserPagingViewModel{
...
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {SecondName} {LastName}";
}
如果您希望全名作为用户对象的一部分,则添加为未映射或使用 HasComputedColumnSql
我想通过使用投影查询(通过 Select)按名称和 return 新 DTO 过滤用户:
var result = context.Users
.Where(user => user.FullName == search)
.Select(u => new UserPagingViewModel
{
Id = u.Id,
IsSearchable = u.IsSearchable,
FirstName = u.FirstName,
LastName = u.LastName,
FullName = u.FullName,
Photo200 = u.Photo200,
City = u.City,
About = u.About,
Skills = u.UserSkills.Select(us => us.Skill.Name),
IsTeamMember = u.UserTeams.Any(ut => ut.UserAction == UserActionEnum.JoinedTeam)
})
.ToList();
用户class:
public class User : IHasId
{
public long Id { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string About { get; set; }
public string Telegram { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public string FullName => FirstName + " " + SecondName + " " + LastName;
public string Photo100 { get; set; }
public string Photo200 { get; set; }
public bool IsModerator { get; set; }
public List<UserTeam> UserTeams { get; set; }
public List<UserSkill> UserSkills { get; set; }
[NotMapped]
public List<Team> TeamsToRecruit { get; set; }
[NotMapped]
public bool AnyTeamOwner { get; set; }
}
数据库是 PostgreSql
。数据提供者 PostgreSQL/Npgsql provider for Entity Framework Core
但是当我尝试执行此请求时,出现异常并显示以下消息:
The LINQ expression 'DbSet<User>() .Where(u => user.FullName == search)' could not be translated. Additional information: Translation of member 'FullName' on entity type 'User' failed. This commonly occurs when the specified member is unmapped. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我不想使用客户评价。 例如,如果我比较 FirstName 属性 一切正常。
请帮我想办法解决这个问题。
您在三个地方有全名。由于您没有全名列(或计算列),因此不能在 dbcontext 查询中使用它。它将生成如下所示的 sql。
SELECT FULLNAME,... FROM USERS WHERE FULLNAME = N"some value".
这就是您收到错误的原因。
您需要从 dbcontext 查询中删除所有 FullName 并像下面那样执行 where 过滤器。
var result = context.Users
.Where(x => (x.FirstName + " " + x.SecondName + " " + x.LastName) == search)
.Select(u => new UserPagingViewModel
{
Id = u.Id,
IsSearchable = u.IsSearchable,
FirstName = u.FirstName,
LastName = u.LastName,
//FullName = u.FullName,
Photo200 = u.Photo200,
City = u.City,
About = u.About,
Skills = u.UserSkills.Select(us => us.Skill.Name),
IsTeamMember = u.UserTeams.Any(ut => ut.UserAction == UserActionEnum.JoinedTeam)
})
.ToList();
你会像下面这样
FROM [Users] AS [t]
WHERE ([t].[FirstName] + N' ' + [t].[SecondName] + N' ' + [t].[LastName]) = N'some value'
然后从用户对象中删除 FullName。
public class User : IHasId
{
public long Id { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string About { get; set; }
public string Telegram { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
// public string FullName => FirstName + " " + SecondName + " " + LastName;
public string Photo100 { get; set; }
public string Photo200 { get; set; }
public bool IsModerator { get; set; }
public List<UserTeam> UserTeams { get; set; }
public List<UserSkill> UserSkills { get; set; }
[NotMapped]
public List<Team> TeamsToRecruit { get; set; }
[NotMapped]
public bool AnyTeamOwner { get; set; }
}
将全名添加到 UserPagingViewModel。
public class UserPagingViewModel{
...
public string FirstName { get; set; }
public string SecondName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {SecondName} {LastName}";
}
如果您希望全名作为用户对象的一部分,则添加为未映射或使用 HasComputedColumnSql