使用实体框架code-第一的方针。
Using Entity Framework code-first approach.
假设我有两个实体类:
[Table("Objects")]
public class DbObject : IValidatableObject
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<DbObjectProperty> Properties { get; set; }
}
[Table("ObjectProperties")]
public class DbObjectProperty
{
public long Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
[Display(Name = "Object"), UIHint("Object")]
public long ObjectId { get; set; }
public virtual DbObject Object { get; set; }
}
要点,这里要注意:
Points to note here:
DBOBJECT
只有一个导航属性,但与外键的任何一列
DbObjectProperty
有一个导航属性的和的一个外键对应的列
这应该是显而易见的是,如果我删除一个对象,我想它的属性去用它,但如果我删除一个单一的财产,我不想消失,整个对象。
DbObject
only has a navigation property, but no column with a foreign key
DbObjectProperty
has a navigation property and a corresponding column with a foreign key
It should be obvious that if I delete an object, I want its properties to go with it, but if I delete a single property, I don’t want the entire object to disappear.
在 OnModelCreating
法为DB的背景下,到现在为止,我有以下定义的关系:
In the OnModelCreating
method for the DB context, up until now I had the following to define the relationship:
modelBuilder.Entity<DbObjectProperty>()
.HasRequired(op => op.Object)
.WithMany(obj => obj.Properties)
.HasForeignKey(op => op.ObjectId)
.WillCascadeOnDelete(false);
当然,
,这意味着没有级联会出现删除。我的问题是:如果我更改为真
,这是否做我想要什么?请记住,如果我删除一个对象,我想它的属性去用它,但如果我删除一个单一的财产,我不想消失,整个对象。
Of course, this means no cascaded deletes will occur. My question is: if I change this to true
, does that do what I want? Remember if I delete an object, I want its properties to go with it, but if I delete a single property, I don’t want the entire object to disappear.
自动生成的迁移code为这种变化(从假
到真
)是这样的:
The auto-generated migration code for this change (from false
to true
) is this:
DropForeignKey("ObjectProperties", "ObjectId", "Objects");
DropIndex("ObjectProperties", new[] { "ObjectId" });
AddForeignKey("ObjectProperties", "ObjectId", "Objects", "Id", cascadeDelete: true);
CreateIndex("ObjectProperties", "ObjectId");
我很担心,这似乎意味着,删除属性将删除其关联的对象。会吗?
I am worried that this seems to imply that deleting a property will delete its associated object. Will it?
推荐答案
级联的方向的数据库中的关系删除是由什么是主要的(主表/唯一密钥)来定义,什么是相关的(表这种关系的外键)的。
The direction of Cascading Delete of a relationship in the database is defined by what is the principal (table of the primary/unique key) and what is the dependent (table of the foreign key) of this relationship.
然后,如果你删除它具有对应于主体的主/唯一键值的外键值的所有家属都将被删除,以及校长。有一个从依赖的方向没有级联删除本金。
Then, if you delete the principal all dependents which have a foreign key value corresponding to the primary/unique key value of that principal are deleted as well. There is no cascading delete in the direction from dependent to principal.
在你的情况主要是 DBOBJECT
和相关的 DbObjectProperty
,因为它是实体/表与外键。
In your case the principal is DbObject
and the dependent is DbObjectProperty
because it is the entity/table with the foreign key.
这将很少有意义有级联删除周围的其他方法 - 尤其是在x一对多的关系:如果删除的依赖( DbObjectProperty
)和委托人( DBOBJECT
)将被自动删除,如果有任何其他的依赖。参见主要是将要删除的外键约束会被侵犯。
It would rarely make sense to have a cascading delete the other way around - especially in x-to-many relationships: If you delete a dependent (DbObjectProperty
) and the principal (DbObject
) would be deleted automatically, a foreign key constraint would be violated if there is any other dependent refering to the principal which is going to be deleted.
您不必担心。
相关推荐
最新文章