简体   繁体   English

实体框架级联删除

[英]Entity Framework cascading delete

I have big problems with cascading deletes in Entity Framework 6. I have a simple database with just 2 tables, persons and addresses, each person can have 0 or many addresses -> 1:n relationship. 在Entity Framework 6中级联删除时,我遇到了很大的问题。我有一个仅包含2个表,人员和地址的简单数据库,每个人可以具有0或许多地址-> 1:n关系。

When I delete a person all addresses are also deleted -> GREAT! 当我删除一个人时,所有地址也将被删除->好!

But when I try to delete all addresses of a person I get an exception: 但是当我尝试删除一个人的所有地址时,我得到一个例外:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系。 When a change is made to a relationship, the related foreign-key property is set to a null value. 对关系进行更改时,相关的外键属性将设置为空值。 If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. 如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

I have searched the internet for the last 2 days and the only thing I found that should work was to create a combined primary key, but this also does not work for me. 我搜索了过去两天的互联网,发现唯一可行的方法是创建一个组合的主键,但这对我也不起作用。

I have written a short demo http://1drv.ms/1uKQTiR that demonstrates whats going on. 我已经写了一个简短的演示http://1drv.ms/1uKQTiR来演示发生了什么。 Maybe anyone can have a look at it and give me some good advice! 也许任何人都可以看一下并给我一些好的建议!

Here is the code: 这是代码:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<Address> Addresses { get; set; }

    public Person()
    {
        Addresses = new List<Address>();
    }
}

public class Address
{
    public int Id { get; set; }
    public string City { get; set; }

    public Person Person { get; set; }

    public Address()
    {
    }
}

 public class Context:DbContext
{
    public DbSet<Person> Persons    { get; set; }
    public DbSet<Address> Addresses { get; set; }

    public Context(): base(@"server=.\SQLEXPRESS;Database=Pers;Trusted_Connection=True;")
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().HasMany(p => p.Addresses).WithRequired(p => p.Person).WillCascadeOnDelete(true);


        base.OnModelCreating(modelBuilder);
    }
}

Code to Delete: 删除代码:

        Person p = _ctx.Persons.Include(x => x.Addresses).FirstOrDefault();
        p.Addresses.Clear();
        _ctx.SaveChanges();

If you use .Clear() or .Remove() you are orphaning the Address object, while not deleting it. 如果你使用.Clear().Remove()你成为孤儿的地址对象,而不是将其删除。 And in your Address table you probably have a required column to store the person-id. 在“地址”表中,您可能有一个必填列来存储人员ID。 The person-id is set to null or 0 and that is probably not a valid value. person-id设置为null或0,并且可能不是有效值。

You have to actually delete the adress. 您必须实际删除地址。 Your adress cannot exist without a person, and that is what you are trying to force when you use .Clear() or .Remove() without deleteing the record. 您ADRESS不能存在没有一个人,那就是你正在尝试当您使用强制什么.Clear().Remove()而不在delete一个记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM