Mik*_*ain 1 c# asp.net-mvc entity-framework entity-framework-core asp.net-core
我希望用户拥有许多关注者和他们关注的许多人.
现在我得到这个错误.
无法确定由"ICollection"类型的导航属性"ApplicationUser.Following"表示的关系.手动配置关系,或从模型中忽略此属性.
// ApplicationUser File.
public class ApplicationUser : IdentityUser<int>
{
// OTHER PROPERTIES ABOVE
public virtual ICollection<ApplicationUser > Following { get; set; }
public virtual ICollection<ApplicationUser > Followers { get; set; }
}
// Context File
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
你需要一个额外的课来完成它.这样的事情应该有效:
public class ApplicationUser : IdentityUser<int>
{
public string Id {get; set;}
// OTHER PROPERTIES ABOVE
public virtual ICollection<UserToUser> Following { get; set; }
public virtual ICollection<UserToUser> Followers { get; set; }
}
public class UserToUser
{
public ApplicationUser User { get; set;}
public string UserId { get; set;}
public ApplicationUser Follower { get; set;}
public string FollowerId {get; set;}
}
// Context File
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<UserToUser>()
.HasKey(k => new { k.UserId, k.FollowerId });
modelBuilder.Entity<UserToUser>()
.HasOne(l => l.User)
.WithMany(a => a.Followers)
.HasForeignKey(l => l.UserId);
modelBuilder.Entity<UserToUser>()
.HasOne(l => l.Follower)
.WithMany(a => a.Following)
.HasForeignKey(l => l.FollowerId);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1503 次 |
| 最近记录: |