简体   繁体   English

实体框架5 - 删除子记录

[英]Entity Framework 5 - Deleting Child Records

VS 2012 (11.0.60315.01 Update 2), C# 5, Entity Framework 5.0.0.0 (Runtime v4.0.30319) VS 2012(11.0.60315.01 Update 2),C#5,Entity Framework 5.0.0.0(Runtime v4.0.30319)

I know similar questions have been posted in the past, but there does not seem to be an answer. 我知道过去曾发布类似的问题,但似乎没有答案。 I think I understand the error, but I'm more interested in finding the 'desired' solution. 我想我理解错误,但我更感兴趣的是找到“理想的”解决方案。 All I want to do is delete a record from the Child table. 我想要做的就是从Child表中删除一条记录。 Can anyone help? 有人可以帮忙吗?

This is a full and simple example. 这是一个完整简单的例子。 The SaveChanges() throws the following exception: SaveChanges()抛出以下异常:

"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." “操作失败:由于一个或多个外键属性不可为空,因此无法更改关系。当对关系进行更改时,相关的外键属性将设置为空值。 foreign-key不支持空值,必须定义新关系,必须为foreign-key属性分配另一个非null值,或者必须删除不相关的对象。“

The code looks like this: 代码如下所示:

using System.Linq;

namespace EFDeleteTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (EFTestEntities context = new EFTestEntities())
            {
                var parent = context.Parents.Single(p => p.Id == 1);

                var child = parent.Children.Single(c => c.Id == 1);

                parent.Children.Remove(child);

                context.SaveChanges(); // Throws the above Exception
            }
        }
    }
}

The Database looks like this: 数据库看起来像这样:

Parent
    Id   (PK, int, not null)   IDENTITY
    Name (nvarchar(50), null)

Child
    Id       (PK, int, not null)  IDENTITY
    ParentId (FK, int, not null)  -- Foreign Key to the Parent Table (Id column))
    Name     (nvarchar(50), null)

There is one record in the Parent table (Id = 1) and there are 2 records in the Child table (Id's 1 and 2). Parent表中有一条记录(Id = 1),Child表中有2条记录(Id为1和2)。 Both of the Child records have a ParentId = 1. 两个子记录都有ParentId = 1。

If all you want is to delete the child object, the parent object should not be concerned in the logic. 如果您只想删除子对象,则不应该在逻辑中关注父对象。 Try the code below, let me know what happens (Not tested). 试试下面的代码,让我知道会发生什么(未经测试)。

using System.Linq;

namespace EFDeleteTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (EFTestEntities context = new EFTestEntities())
            {                      
                var child = context.Children.Single(c => c.Id == 1);

                context.Children.Remove(child);

                context.SaveChanges();
            }
        }
    }
}

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

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