我下面這個教程來實現與EF核心1.1我的友誼系統:http://www.codedodle.com/2014/12/social-network-friends-database.html如何實現自引用與Entity Framework Core 1.1的多對多關係?
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
無法確定由類型的導航屬性 'Friendship.ActionUser' 'ApplicationUser' 所代表的關係。要麼手動配置關係,要麼忽略模型中的該屬性。
另外,由於EF Core的移動速度很快,我發現有許多不同的方法可以做到這一點。我不確定自己的多對多關係的實現,有人能給我一些建議嗎?
- 如何定義Friendship.ActionUser和ApplicationUser之間的關係?
- 任何關於什麼是實現這個自引用多對多關係的正確方法的建議? EF Core的移動速度很快,我在網上發現了很多不同的方式,但它們看起來過時了
謝謝! :)
部分由此提交的錯誤https://github.com/aspnet/EntityFramework/issues/8886 – Smit