none
cascade delete EF Code First RRS feed

  • Question

  • I'm new with entity framework code first, so I will like to ask the following: I have this entity:

    public class ConfigurationSetEntity 
    { 
       public virtual List<IsapreEntity> Isapres { get; set; } 
       [Key] 
       [DatabaseGenerated(DatabaseGeneratedOption.None)] 
       public virtual string Culture { get; set; } 
    }

    and also this:

    public class IsapreEntity 
    { 
        [Key] 
        [DatabaseGenerated(DatabaseGeneratedOption.None)] 
        [Required] 
        public virtual string IsapreName { get; set; } 
     
        [ForeignKey("IsapreOf")] 
        [Required] 
        public virtual string CultureId { get; set; } 
     
        public virtual ConfigurationSetEntity IsapreOf { get; set; } 
    }

    when I use ConfigurationSetEntity.Isapres.Remove(entity) I get an DbEntityValidationException, this is the code for the remove:

    IsapreModel original = this.ChangeSet.GetOriginal(currentIsapre); 
    ConfigurationSetEntity defaultConfigSet = dbContext.ConfigurationSets.Find(Constants.DefaultConfigurationSetId); 
            IsapreEntity originalEntity =defaultConfigSet.Isapres.Find(e => e.IsapreName==original.Isapre); 
            defaultConfigSet.Isapres.Remove(originalEntity); 
     
            try 
            { 
                dbContext.SaveChanges(); 
            } 
            catch (Exception ex) 
            { 
              //This is where I catch the exception 
            } 
            defaultConfigSet.Isapres.Add(new IsapreEntity 
                                             { 
                                                IsapreName = currentIsapre.Isapre 
                                             }); 
            try 
            { 
                dbContext.SaveChanges(); 
            } 
            catch (Exception ex) 
            { 
     
            } 
    

    And this is the exception:      "The IsapreOf field is required." What I want to do is to use ConfigurationSetEntity.Isapres.Remove(entity) and have entity removed from the IsapreEntities table and from the list. Can some one please explain why the error and/or how can I achive my intended purpose?

    Thanks in advance

    Juan Carlos


    Wednesday, August 22, 2012 7:47 PM

Answers

  • Hi Juan,

    Welcome to the MSDN forum.

    ConfigurationSetEntity and IsapreEntity is the one to many relationship. If you remove a IsapreEntity, its CultureId and IsapreOf properties will be null. Since these properties are required, the exception will occur. You can remove this IsapreEntity from dbcontext before call SaveChanges() like:

    dbContext.IsapreSet.Remove(IsapreEntity);

    If I misunderstood anything, please feel free to let me know.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Thursday, August 30, 2012 1:27 AM
    Friday, August 24, 2012 7:13 AM

All replies

  • Hi Juan,

    Welcome to the MSDN forum.

    ConfigurationSetEntity and IsapreEntity is the one to many relationship. If you remove a IsapreEntity, its CultureId and IsapreOf properties will be null. Since these properties are required, the exception will occur. You can remove this IsapreEntity from dbcontext before call SaveChanges() like:

    dbContext.IsapreSet.Remove(IsapreEntity);

    If I misunderstood anything, please feel free to let me know.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Thursday, August 30, 2012 1:27 AM
    Friday, August 24, 2012 7:13 AM
  • Thanks a lot Alexander, your solution is working perfectly well, thank you.

    Thursday, August 30, 2012 8:53 PM