【发布时间】:2019-01-29 19:05:56
【问题描述】:
我有建立多对多关系的类。
public class Chat
{
[Key]
public Guid Id { get; set; }
public ICollection<ApplicationUserChat> UserChats { get; set; }
}
public class ApplicationUserChat
{
[Required]
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public Guid ChatId { get; set; }
public Chat Chat { get; set; }
}
public class ApplicationUser : IdentityUser
{
public ICollection<ApplicationUserChat> UserChats { get; set; }
}
例如,我有一个user1 和user2
- 我需要为
user1选择聊天,其中聊天包含user2 -
我需要为
user1选择聊天,其中聊天包含user2并且不包含其他用户(例如一个聊天仅包含 2 个参与者)如何通过一个对 DB 的请求在 EF Core 中执行此操作?
【问题讨论】:
-
不应该
UserId在ApplicationUser上吗? -
@devNull,是的,applicationUser 包含 UserId
-
第一个:首先,将
UserId的user1和user2放入字符串列表 -userIdList,然后是dbContext.Chat.Where(x => x.UserChats.Any(y => userIdList.Contains(y.UserId)))。我建议使用用户 ID 的原因是为了避免在不同数据库上下文中获取查询结果时产生混淆。 -
您需要在一个请求中同时获取 1 和 2 还是每个案例一个请求?
标签: c# entity-framework .net-core entity-framework-core asp.net-core-2.1