【发布时间】:2017-06-15 16:53:27
【问题描述】:
我正在按照本教程使用 EF Core 1.1 实现我的友谊系统:http://www.codedodle.com/2014/12/social-network-friends-database.html
Friendship.cs
public class Friendship
{
public Guid ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public Guid FriendId { get; set; }
public ApplicationUser Friend { get; set; }
public StatusCode Status { get; set; }
public Guid ActionUserId { get; set; }
public ApplicationUser ActionUser { get; set; }
public byte[] Timestamp { get; set; }
}
public enum StatusCode
{
Pending = 0,
Accepted = 1,
Declined = 2,
Blocked = 3
}
ApplicationUser.cs
public class ApplicationUser : IdentityUser<Guid>
{
...
public ICollection<Friendship> FriendRequestsMade { get; set; }
public ICollection<Friendship> FriendRequestsAccepted { get; set; }
public byte[] Timestamp { get; set; }
}
MyDbContext.cs
public class SocialCircleContext : IdentityDbContext<ApplicationUser, Role, Guid>
{
builder.Entity<Friendship>()
.HasIndex(x => new { x.ApplicationUserId, x.FriendId })
.IsUnique();
builder.Entity<Friendship>()
.HasOne(x => x.ApplicationUser)
.WithMany(y => y.FriendRequestsMade)
.HasForeignKey(x => x.ApplicationUserId).OnDelete(DeleteBehavior.Restrict);
builder.Entity<Friendship>()
.HasOne(x => x.Friend)
.WithMany(y => y.FriendRequestsAccepted)
.HasForeignKey(x => x.FriendId);
}
添加迁移 InitialMigration 的结果
无法确定“ApplicationUser”类型的导航属性“Friendship.ActionUser”所代表的关系。要么手动配置关系,要么从模型中忽略此属性。
此外,随着 EF Core 的快速发展,我找到了许多不同的方法来做到这一点。我不确定我的自引用多对多关系的实现,有人可以给我一些建议吗?
- 如何定义 Friendship.ActionUser 和 ApplicationUser 之间的关系?
- 对于实现这种自引用多对多关系的正确方法有什么建议吗? EF Core 发展很快,我在网上找到了很多不同的方法,但它们似乎已经过时了
谢谢! :)
【问题讨论】:
标签: c# asp.net-core many-to-many entity-framework-core self-referencing-table