none
Entity Framework (Fluent) deleting an entity without setting foreign key as null RRS feed

  • Question

  • 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);
            }
    
    

    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.

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

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

    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.)

    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.






    • Edited by raghavcinch Wednesday, July 22, 2015 5:40 AM
    Wednesday, July 22, 2015 5:33 AM

All replies

  • Hi raghavcinch,

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

    Could you share the code about how to complete the EntityContainer? and how to do in repo.GetByID?

    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.

    Regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 24, 2015 3:02 AM