0
我使用EF代碼第一種方法,有一個領域模型類爲:如何更新實體框架中的鏈接表
public class WindmillUser
{
public WindmillUser()
{
AssignedMsos = new Collection<Mso>();
RepackerHeaders = new Collection<RepackerHeader>();
}
public int WindmillUserId {get;set;}
public string Username { get; set; }
public RoleType Role { get; set; }
public string Email { get; set; }
public bool Active { get; set; }
public DateTime? LastInteraction { get; set; }
public string TelephoneNumber { get; set; }
public virtual ICollection<Mso> AssignedMsos { get; set; }
public virtual ICollection<RepackerHeader> RepackerHeaders { get; set; }
public virtual ICollection<WindmillUser> WindmillUserImpersonateParent { get; set; }
public virtual ICollection<WindmillUser> WindmillUsersImpersonateChild { get; set; }
}
}
我在DataContext類以下配置:
modelBuilder.Entity<WindmillUser>().HasMany(entity => entity.WindmillUsersImpersonateChild).WithMany(child => child.WindmillUserImpersonateParent)
.Map(map =>
{
map.ToTable("WindmilUserImpersonate");
map.MapLeftKey("WindmillUserImpersonateParentId");
map.MapRightKey("WindmillUsersImpersonateChildId");
});
這將在數據庫中創建一個表WindmillUserImpersonate,該數據庫存儲WindmillUser表與其自身的多對多關係。
我想在WindmillUserImpersonate表中插入記錄。我是否需要爲WindmillUserImpersonate創建一個域模型來執行操作,如果是,那麼以下域是否正確?
public class WindmillUserImpersonate
{
public int WindmillUserImpersonateParentId { get; set; }
public int WindmillUsersImpersonateChildId { get; set; }
public virtual WindmillUser WindmillUser { get; set; }
}
如果沒有必要創建一個WindmillUserImpersonate類,那麼我怎麼才能插入/更新WindmillUserImpersonate表?
遺憾的是它沒有工作。我必須更改域模型並創建另一個實體WindmillUserImpersonate,然後將它添加到dbcontext類中,如下所示:modelBuilder.Entity()。HasKey(u => new {u.WindmillUserImpersonateParentId,u.WindmillUsersImpersonateChildId}); –