[英]Many to one relation with attached entity
I have a many to many relation that I want to update using a detached entity that I have attached to the context (deserilized from WebApi). 我有一个多对多关系,我想使用一个附加到上下文的分离实体(从WebApi中删除)来更新。
I have created this helper method that works for Many to one relations 我创建了一种适用于多对一关系的辅助方法
public void SetEntityState<TEntity>(IEnumerable<TEntity> source) where TEntity : EntityBase, IDeletable
{
foreach (var deletable in source.ToList())
{
var entry = Entry(deletable);
if (deletable.Deleted)
entry.State = EntityState.Deleted;
else
entry.State = deletable.Id == 0 ? EntityState.Added : EntityState.Modified;
}
}
Used like 使用像
db.Entry(user).State = EntityState.Modified;
db.SetEntityState(user.HomeFolders); //Works because HomeFolders is a many to one relation
db.SetEntityState(user.Roles) //Does not work because Roles is a many to many relation
This does not ofcourse work with a Many to many relation because entry.State = EntityState.Deleted
will point to the referenced enity and try to delete that instead of the row in the relation table. 这当然不适用于多对多关系,因为entry.State = EntityState.Deleted
将指向引用的实体,并尝试删除该实体,而不是删除关系表中的行。
So how can I delete/add many to many relations on a newly attached entity? 那么,如何删除/添加新连接实体上的多对多关系?
edit: Config 编辑:配置
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
HasKey(u => u.Id);
Property(u => u.Username)
.IsRequired()
.HasMaxLength(50);
Property(u => u.Password)
.IsRequired()
.HasMaxLength(128);
HasMany(s => s.HomeFolders)
.WithRequired()
.Map(
m => m.MapKey("UserId")
);
HasMany(p => p.Roles)
.WithMany()
.Map(m =>
{
m.ToTable("UserRole");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
});
ToTable("User");
}
}
Models 楷模
public abstract class EntityBase
{
public int Id { get; set; }
}
public class Role : EntityBase, IDeletable
{
public string Name { get; set; }
public bool Deleted { get; set; }
}
public class User : EntityBase
{
public string Username { get; set; }
public string Password { get; set; }
public virtual ICollection<HomeFolder> HomeFolders { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
update: Delete actually works if I remove the Role after the user is attached. 更新:如果我在附加用户后删除了角色,则删除实际上是可行的。 But new roles that is in teh list will not be added 但是不会添加列表中的新角色
Update code as of latest revision, Add role still not working 更新代码为最新版本,添加角色仍然不起作用
public void UpdateUser(User user)
{
db.Entry(user).State = EntityState.Modified;
db.SetEntityState(user.HomeFolders);
user.Roles.Where(r => r.Deleted).ToList().ForEach(r => user.Roles.Remove(r));
if (string.IsNullOrEmpty(user.Password))
db.Entry(user).Property(x => x.Password).IsModified = false;
else
{
SetPassword(user);
}
db.SaveChanges();
}
Why do you chose the hard path and deal with EntityState
? 为什么选择艰难的道路并处理EntityState
?
Simply remove the roles, if user
is attached: 如果已附加user
,只需删除角色:
user.roles.ToList().ForEach(r=> user.Roles.Remove(r))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.