Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EntityFramework:如何配置级联删除以使外键无效_Entity Framework_Entity Framework 5 - Fatal编程技术网

Entity framework EntityFramework:如何配置级联删除以使外键无效

Entity framework EntityFramework:如何配置级联删除以使外键无效,entity-framework,entity-framework-5,Entity Framework,Entity Framework 5,EntityFramework的文档说明以下行为是可能的: 如果依赖实体上的外键可为空,则代码首先会为空 未在关系上设置级联删除,并且当主体 已删除的外键将设置为空 (来自) 然而,我无法实现这样的行为 我首先使用代码定义了以下实体: public class TestMaster { public int Id { get; set; } public string Name { get; set; } public virtual ICollectio

EntityFramework的文档说明以下行为是可能的:

如果依赖实体上的外键可为空,则代码首先会为空 未在关系上设置级联删除,并且当主体 已删除的外键将设置为空

(来自)

然而,我无法实现这样的行为

我首先使用代码定义了以下实体:

public class TestMaster
{
    public int Id { get; set; }
    public string Name { get; set; }        
    public virtual ICollection<TestChild> Children { get; set; }       
}

public class TestChild
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual TestMaster Master { get; set; }
    public int? MasterId { get; set; }
}

我是做错了什么,还是误解了MSDN所说的话?

它确实按照所描述的那样工作,但有关MSDN的文章没有强调它只在孩子也被加载到上下文中时起作用,而不仅仅是父实体。因此,与使用
Find
(仅加载父对象)不同,您必须使用带有
Include
(或任何其他方式将子对象加载到上下文中)的即时加载:

使用(var dbContext=newtestcontext())
{
var master=dbContext.Set().Include(m=>m.Children)
.SingleOrDefault(m=>m.Id==1);
dbContext.Set().Remove(master);
dbContext.SaveChanges();
}

这将从数据库中删除master,将
子实体中的所有外键设置为
null
,并将子实体的更新语句写入数据库。

在遵循@Slauma的伟大回答后,我仍然得到与OP相同的错误

所以,不要像我一样天真,认为下面的例子会得到同样的结果

dbCtx.Entry(principal).State = EntityState.Deleted;
dbCtx.Dependant.Where(d => d.PrincipalId == principalId).Load();

// code above will give error and code below will work on dbCtx.SaveChanges()

dbCtx.Dependant.Where(d => d.PrincipalId == principalId).Load();
dbCtx.Entry(principal).State = EntityState.Deleted;

在将实体状态设置为“已删除”(如果这样做)之前,首先将子项加载到上下文中。

粗体文本保存了我。延迟加载很好,但请确保在删除之前加载子对象…我正在尝试此示例。但是在“包含”中有一个错误。它表示“无法将lamba表达式转换为类型“string”,因为它不是委托类型”。。。。我错过了什么?谢谢
 using (var dbContext = new TestContext())
        {
            var master = dbContext.Set<TestMaster>().Find(1);
            dbContext.Set<TestMaster>().Remove(master);
            dbContext.SaveChanges();
        }
System.Data.Entity.Infrastructure.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
----> System.Data.UpdateException : An error occurred while updating the entries. See the inner exception for details.
----> System.Data.SqlClient.SqlException : The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.TestChilds_dbo.TestMasters_MasterId". The conflict occurred in database "SCM_Test", table "dbo.TestChilds", column 'MasterId'.
The statement has been terminated.
using (var dbContext = new TestContext())
{
    var master = dbContext.Set<TestMaster>().Include(m => m.Children)
        .SingleOrDefault(m => m.Id == 1);
    dbContext.Set<TestMaster>().Remove(master);
    dbContext.SaveChanges();
}
dbCtx.Entry(principal).State = EntityState.Deleted;
dbCtx.Dependant.Where(d => d.PrincipalId == principalId).Load();

// code above will give error and code below will work on dbCtx.SaveChanges()

dbCtx.Dependant.Where(d => d.PrincipalId == principalId).Load();
dbCtx.Entry(principal).State = EntityState.Deleted;