【问题标题】:Entity Framework Core: issue with Contains methodEntity Framework Core:包含方法的问题
【发布时间】:2017-03-24 03:22:57
【问题描述】:

有我的db简化模型:

public class Chat
{
public ICollection<ApplicationUser> Users {get; set;} //nav property - represents participants of a chat
}

public class ApplicationUser : IdentityUser // it represents a net-identity user; it does not have any references to chats
{...}

因此,在控制器类中,我尝试获取诸如包含当前用户作为参与者的聊天:

var user = GetUser();
_context.Chats.Where(chat => chat.Users.Contains(user)).ToList();

这段代码抛出异常:

您不能使用 ...ApplicationUser 类型的表达式 参数类型 方法“Boolean”的“Microsoft.EntityFrameworkCore.Storage.ValueBuffer” 包含[ValueBuffer](System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Storage.ValueBuffer], Microsoft.EntityFrameworkCore.Storage.ValueBuffer)"

这里有什么问题?

【问题讨论】:

  • 请提供更多关于您的控制器代码的信息。你想把它分配给什么?
  • 实际问题是您试图将对象(“用户”)作为一个洞进行比较。而且由于 EF 在后台转换为 SQL,这将失败。没有用于比较对象的 SQL 函数。最好按主键查找。

标签: asp.net asp.net-core entity-framework-core


【解决方案1】:

你需要使用 Any(),像这样

 var chatsList =_context.Chats.Where(chat => chat.Users.Any(u => u.id== user.id)).ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2019-04-13
    • 2018-11-05
    相关资源
    最近更新 更多