实体框架(Fluent)删除实体而不将外键设置为null [英] Entity Framework (Fluent) deleting an entity without setting foreign key as null

查看:109
本文介绍了实体框架(Fluent)删除实体而不将外键设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有以下一套classess和Fluent Maps。

I have the following set of classess and Fluent Maps.

public partial class TieTrunk 
    {

        public int Id { get; set; }
        public string Name { get; set; }
        public int ClusterId { get; set; }
        public virtual Cluster Cluster { get; set; }

    }
public partial class Cluster
    {
        public int Id { get; set; }
        public string Name { get; set; }
        HasMany(t => t.TieTrunks).WithRequired(d => d.Cluster);
    }



public ClusterMap()
        {
            HasKey(t => t.Id);
            ToTable("tb_cluster");
            ....other fields
            HasMany(t => t.TieTrunks).WithRequired(d => d.Cluster);
        }


 public TieTrunkMap()
        {
            HasKey(t => t.Id);      
            ToTable("tb_tietrunk");
           ....other fields
            HasRequired(t => t.Cluster).WithMany().HasForeignKey(d => d.ClusterId);
        }





现在我尝试使用以下代码从子项(TieTrunk)中删除一行。


Now i try to delete a row from the child (TieTrunk), with following code.

TieTrunk toBeDeleted = repo.GetById(trunkId);
DBContext.EntityContainer.TieTrunks.Remove(toBeDeleted)



在此之后,我尝试操作已删除实体的引用对象。(通过传递此引用在不同文件上)

After this, I try to operate on the deleted entity's referenced object. (On different file by passing this reference)

var cluster = toBeDeleted.Cluster;  //Cluster comes as null
//Perform some operations using the cluster above.
//But getting null reference error.



但是,我可以通过以下代码检索Cluster对象的Id,

However, in the I can retrieve the Id of the Cluster object via the following code,

(toBeDeleted as DbEntityEntry).OriginalValues.GetValue<int>("ClusterId")



有没有一种方法可以阻止EF设置child.AssocaiatedParent = null,当孩子被删除时。(在我的情况下这是不必要的,因为孩子无论如何都要去要删除。)

Is there a way Fluent to stop EF from setting the child.AssocaiatedParent = null, when the child is being deleted. (which is not necessary in my case as the child is anyway going to be deleted.)


注意:数据库字段tb_tie_trunk.ClusterId在SQL中设置为Not Null。由于某种原因,EF在.Net中将相关对象(此处为Cluster)设置为null代码(在context.EntityContainer中)。但是,如果字段不是null
,则保留ClusterId,如果为null,则保留为lost。

Note: The DB field tb_tie_trunk.ClusterId is set to Not Null in the SQL. For some reason, EF is setting the related object (here, Cluster) to null in the .Net code (inside context.EntityContainer). However, the ClusterId is retained if the filed is not null and lost if it is null.

推荐答案

嗨raghavcinch,

Hi raghavcinch,

- >
TieTrunk toBeDeleted
= repo GetById trunkId );

DBContext EntityContainer TieTrunks 删除 toBeDeleted

--> TieTrunk toBeDeleted = repo.GetById(trunkId);
DBContext.EntityContainer.TieTrunks.Remove(toBeDeleted)

你能分享有关如何完成EntityContainer的代码吗?以及如何在repo.GetByID中进行操作?

顺便说一句,你说当孩子被删除时,它会将孩子.AssocaiatedParent = null。这是EF的默认行为,如果可能的话,你可以提交这个功能请求:
< span style ="font-family:'Calibri',sans-serif; font-size:11pt"> http:/ /data.uservoice.com/forums/72025-entity-framework-feature-suggestions
Entity Framework产品团队正在那里收听用户语音。您可以发送你的想法在那里,人们可以投票。如果你提交这个建议,你可以在这里发布该链接,我会投票。谢谢你的理解。

And by the way, you said when the child is being deleted, it will the child.AssocaiatedParent = null. It is a default behavior of EF, If possible, you could submit this feature request: http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions. The Entity Framework product team is listening to user voice there. You can send your idea there and people can vote. If you submit this suggestion, you might post that link here, I will vote it. Thanks for your understanding.

问候,

Youjun Tang

Regards,
Youjun Tang


这篇关于实体框架(Fluent)删除实体而不将外键设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆