DbSet.Remove和DbContext.Entry(entity)之间的区别.State = EntityState.Deleted [英] Difference between DbSet.Remove and DbContext.Entry(entity).State = EntityState.Deleted
问题描述
考虑以下实体模型:
public class Agreement : Entity
{
public int AgreementId { get; set; }
public virtual ICollection<Participant> Participants { get; set; }
}
public class Establishment : Entity
{
public int EstablishmentId { get; set; }
}
public class Participant : Entity
{
public int AgreementId { get; set; }
public virtual Agreement Agreement { get; set; }
public int EstablishmentId { get; set; }
public virtual Establishment { get; set; }
public bool IsOwner { get; set; }
}
没有直接的$ code> ICollection<协议实体的建立> 是因为IsOwner属性,它进一步定义了这种多对多关系。
The reason there is not a direct ICollection<Establishment>
on the Agreement entity is because of the IsOwner property, which further defines this many-to-many relationship.
关系映射如下所示:
internal ParticipantOrm()
{
ToTable(typeof(Participant).Name);
HasKey(k => new { k.AgreementId, k.EstablishmentId });
HasRequired(d => d.Agreement)
.WithMany(p => p.Participants)
.HasForeignKey(d => d.AgreementId)
.WillCascadeOnDelete(true);
HasRequired(d => d.Establishment)
.WithMany()
.HasForeignKey(d => d.EstablishmentId)
.WillCascadeOnDelete(true);
}
关系是单向的,这意味着你只能从在协议中 - 您无法访问该机构的协议。
The relationship is uni-directional, meaning you can only get to the Participants from within an Agreement -- you cannot access the agreements from the Establishment.
现在,由于协议实体的主键是gerund Participant的主键的一部分,并且由于级联删除具体来说,我希望将协议置于已删除状态也将导致其参与者收集中的每个实体也被置于已删除状态。
Now, since the Agreement entity's primary key is part of the gerund Participant's primary key, and since cascade delete is specified, I would expect that putting the Agreement into the Deleted state would also cause each entity in its Participants collection to also be put into the deleted state.
在协议上调用删除时工作DbSet:
This appears to work when calling Remove on an Agreement DbSet:
// this works
dbContext.Agreements.Remove(agreement);
dbContext.SaveChanges();
但是,当简单地将协议条目的状态设置为删除时,似乎不起作用:
However, it does not appear to work when simply setting the agreement entry's state to deleted:
// this causes an exception when Participants is not empty
dbContext.Entry(agreement).State = EntityState.Deleted;
dbContext.SaveChanges();
有没有一种方法可以定义关系,以便将协议实体简单地放入删除状态还会导致相应的收集项目实体被置于删除状态?
更新
我一直在阅读这篇文章,发现渴望加载参与者收藏没有帮助:
I have been reading about this, and found out that eager loading the Participants collection does not help:
// eager loading does not help, this still breaks
var agreement = dbContext.Set<Agreement>()
.Include(a => a.Participants)
.FirstOrDefault();
dbContext.Entry(agreement).State = EntityState.Deleted;
dbContext.SaveChanges();
推荐答案
通过调用以下内容解决这个问题: p>
Ended up solving this by calling the following:
dbContext.Set<Agreement>().Remove(agreement);
我想摆脱协议
属性在 DbContext
,这就是为什么我试图用 Entry(agreement).State = EntityState.Deleted
。
I wanted to get rid of the Agreements
property on the DbContext
, which is why I was trying to do it with Entry(agreement).State = EntityState.Deleted
.
这篇关于DbSet.Remove和DbContext.Entry(entity)之间的区别.State = EntityState.Deleted的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!