从EF实体中删除关联实体

时间:2011-11-03 21:06:38

标签: c# asp.net entity-framework

我有许多其他表连接到它的enitity。我想要做的是,在我创建某个实体的实例后,删除所有将其连接到其他实体/表的属性。这可能吗?感谢

1 个答案:

答案 0 :(得分:0)

我相信你只需为单个导航属性指定null并在子导航属性上调用Clear:

Employee
{       
    SomeOtherEntity SomeOtherEntityNavigation { get; set;}    
    ICollection<Blah>  Blahs {get; set;}    
}    

//somewhere
anEmployee.SomeOtherEntityNavigation = null;
anEmployee.Blahs.Clear();

然后保存您的DBContext。

另请注意,当您最初创建实体的实例时,它将与其他实体没有任何关系,除非您在数据层或自动关联新实体的数据库中有某些魔法。

相关问题