C# EF代码第一个自定义表问题使用外键级联删除
因此,我在数据库中使用一个名为MyEntity的实体。还有另一个表显示MyEntity实体的详细信息,称为MyEntityDetail。这种关系是一对多的,即MyEntity记录可以有多个MyEntityDetails。但是,DB表的设计很差,我无法改变这一点。表定义如下所示:C# EF代码第一个自定义表问题使用外键级联删除,c#,entity-framework,ef-code-first,cascade,C#,Entity Framework,Ef Code First,Cascade,因此,我在数据库中使用一个名为MyEntity的实体。还有另一个表显示MyEntity实体的详细信息,称为MyEntityDetail。这种关系是一对多的,即MyEntity记录可以有多个MyEntityDetails。但是,DB表的设计很差,我无法改变这一点。表定义如下所示: MyEntity: myentity_id (PK) property_1 property_2 MyEntityDetail: myentity_id (nullable) property_3 property_4
MyEntity:
myentity_id (PK)
property_1
property_2
MyEntityDetail:
myentity_id (nullable)
property_3
property_4
public MyEntityMapping(string schema = "dbo")
{
ToTable(schema + ".MyEntity");
HasKey(e => e.EntityId);
Property(e => e.EntityId).HasColumnName("myentity_id").IsRequired();
Property(e=> e.PropertyOne).HasColumnName("property_1").IsRequired();
Property(e=> e.PropertyTwo).HasColumnName("property_2").IsRequired();
HasMany(e=> e.MyEntityDetail)
.WithOptional()
.HasForeignKey(e=> e.EntityId)
.WillCascadeOnDelete(false);
}
public MyEntityDetailMapping(string schema = "dbo")
{
ToTable(schema + ".MyEntityDetail");
HasKey(e => new {e.EntityId, e.PropertyFour});
Property(e => e.EntityId).HasColumnName("myentity_id").IsRequired();
Property(e=> e.PropertyThree).HasColumnName("property_3").IsRequired();
Property(e=> e.PropertyFour).HasColumnName("property_4").IsRequired();
}
我首先与EF和code合作。我的班级定义如下:
public class MyEntity
{
public int EntityId {get; set;}
public int PropertyOne {get; set;}
public int PropertyTwo {get; set;}
public virtual ICollection<MyEntityDetail> SomeDetails {get; set;}
}
public class MyEntityDetail
{
public int EntityId {get; set;}
public int PropertyThree {get; set;}
public int PropertyFour {get; set;}
}
我试图完成的是从MyEntity表中查询某些选定的实体,并将它们连同各自的MyEntityDetails一起删除。然而,我遇到了几个问题。我的做法如下:
MyEntity:
myentity_id (PK)
property_1
property_2
MyEntityDetail:
myentity_id (nullable)
property_3
property_4
public MyEntityMapping(string schema = "dbo")
{
ToTable(schema + ".MyEntity");
HasKey(e => e.EntityId);
Property(e => e.EntityId).HasColumnName("myentity_id").IsRequired();
Property(e=> e.PropertyOne).HasColumnName("property_1").IsRequired();
Property(e=> e.PropertyTwo).HasColumnName("property_2").IsRequired();
HasMany(e=> e.MyEntityDetail)
.WithOptional()
.HasForeignKey(e=> e.EntityId)
.WillCascadeOnDelete(false);
}
public MyEntityDetailMapping(string schema = "dbo")
{
ToTable(schema + ".MyEntityDetail");
HasKey(e => new {e.EntityId, e.PropertyFour});
Property(e => e.EntityId).HasColumnName("myentity_id").IsRequired();
Property(e=> e.PropertyThree).HasColumnName("property_3").IsRequired();
Property(e=> e.PropertyFour).HasColumnName("property_4").IsRequired();
}
从MyEntity表中查询并获取多个EntityId的列表
从MyEntityDetails表中查询并获取几个MyEntityDetail对象的列表。
通过执行:MyContext.EntrymyEntity.State=EntityState.Deleted擦除步骤1中获得的对象;
按照步骤3中的步骤擦除步骤2中获得的对象。
然而,问题是一个MyEntity可以有几个完全相同的MyEntityDetails。因此,当EF尝试基于映射中定义的主键附加对象时,我得到一个异常,该异常表示相同类型的另一个实体已经具有相同的主键值
我的想法是以某种方式更改实体的映射,并允许级联删除,以便仅查询MyEntity实体并执行删除操作,这也将删除相关的MyEntityDetail对象。然而,我不知道如何做到这一点
谢谢 就EF而言,恐怕你在这里迷路了。EF无论如何都需要一个唯一的密钥。如果细节在Id+其他属性上都不是唯一的,那么就没有办法做到这一点。您甚至不能在详细信息中添加标识列吗?我无法以任何方式接触数据库设计。您可以执行ExecuteSQLCommand。