因此,我有以下3个课程:
User.cs
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual Timezone Timezone { get; set; }
public virtual ICollection<UserChannel> UserChannels { get; set; }
}
Channel.cs
public class Channel
{
public int Id { get; set; }
[Column("Channel")]
public string Name { get; set; }
}
UserChannel.cs
public class UserChannel
{
public int Id { get; set; }
public virtual User User { get; set; }
public virtual Channel Channel { get; set; }
}
UserChannel
是之间的介质User
和Channel
。
该UserChannels
表如下所示:
+----+--------+-----------+
| Id | UserId | ChannelId |
+----+--------+-----------+
无论UserId
和ChannelId
外键,用于User
和Channel
类。
现在,要获取Channels
属于用户的内容,我必须编写如下内容:
using var context = new TwitchContext();
var me = context.Users.FirstOrDefault(x => x.Id == 1);
var myChannels = me.UserChannels.Select(x => x.Channel);
有没有办法我可以直接获取Channel
而不是使用的集合UserChannel
?
根据当前路线图,在没有引用联接表/实体的情况下,多对多关系将要等到EF Core 5发布后才能使用:
此问题跟踪功能请求:
https://github.com/dotnet/efcore/issues/19003
包含在EF Core 5.0.0里程碑中。
EF Core 5计划于2020年11月发布。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句