[英]How to update a linked table in entity framework
I am using EF code first approach and have a domain model class as: 我正在使用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; }
}
}
I have the following configuration in the datacontext class: 我在datacontext类中具有以下配置:
modelBuilder.Entity<WindmillUser>().HasMany(entity => entity.WindmillUsersImpersonateChild).WithMany(child => child.WindmillUserImpersonateParent)
.Map(map =>
{
map.ToTable("WindmilUserImpersonate");
map.MapLeftKey("WindmillUserImpersonateParentId");
map.MapRightKey("WindmillUsersImpersonateChildId");
});
This creates a table WindmillUserImpersonate in the database which stores the many to many relationship of WindmillUser table to itself. 这将在数据库中创建一个表WindmillUserImpersonate,该表存储WindmillUser表与其自身的多对多关系。
I want to insert the records in WindmillUserImpersonate table. 我想在WindmillUserImpersonate表中插入记录。 Do I need to create a domain model for WindmillUserImpersonate to do operations and if yes then is the following domain correct?
我需要为WindmillUserImpersonate创建一个域模型来执行操作,如果是,那么以下域正确吗?
public class WindmillUserImpersonate
{
public int WindmillUserImpersonateParentId { get; set; }
public int WindmillUsersImpersonateChildId { get; set; }
public virtual WindmillUser WindmillUser { get; set; }
}
And if there is no need to create a WindmillUserImpersonate class then how can I Insert/update the WindmillUserImpersonate table? 而且,如果不需要创建WindmillUserImpersonate类,该如何插入/更新WindmillUserImpersonate表?
If you have entity WindmillUser user and want to add new parent childUser, you should do: 如果您具有实体WindmillUser用户并想要添加新的父childUser,则应该执行以下操作:
user.WindmillUsersImpersonateChild.Add(childUser);
context.SaveChanges();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.