开发者

How to delete child entities in an InverseProperty relationship using code first entity framework 4.1

I have a model and I am having trouble managing the relationships using entity framework 4.1

Here the model (simplified):

    public class UserConfig {
        [Key]
        [Column("id", TypeName = "bigint")]
        public long Id { get; set;}

        [InverseProperty("UserConfigId")]
        public virtual List<ColumnConfig> ColumnConfigs { get; set; }
    }

    public class ColumnConfig {
        [Key]
        [Column("id", TypeName = "bigint")]
        public long Id { get; set; }

        [Column("user_config_id", TypeName = "bigint")]
        public long UserConfigId { get; set; }

        [Column("width", TypeName = "int")]
        public int Width{ get; set; }

        [Column("col_name", TypeName = "varchar")]
        public string ColumnName{ get; set; }
    }

The model represents a user and a custom view of a table of data within a UI. They resize the columns the way they want and then save their settings. I have a webservice that accepts a list of the columns they want and their respective widths.

The problem I am having is updating the user's ColumnConfigs within my web service. The webservice does not receive the ColumnConfig id's, so my approach has been to try and first delete all the existing ColumnConfigs for the user, and then second create a new set of objects according to the new values passed in.

I can't manage to delete any of the ColumnConfig objects. Here's my code:

public void UpdateUserConfig(UserConfig uc) {

    UserConfig origUserConf = ctx.ColumnConfigs.Find(new object[] {uc.Id});

        origUserConf.ForEach(uc => ctx.ColumnConfigs.Remove(uc));  // remove old

        origUserConf.ColumnConfigs = uc.ColumnConfigs;   // add new

    ctx.SaveChanges();  
}

This isn't working, it gives the error:

System.InvalidOperationException: The ope开发者_如何学运维ration failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()

I'm not sure why is thinks there is a null constraint here. I haven't specified any fields as being required.


The easiest way to remove child relationships would be to invoke:

origUserConfig.ColumnConfigs.Clear();

Unfortunately, this wouldn't work for you for two reasons: (1) You have a non-nullable relationship defined between your Parent and Child entity, and (2) even if the relationship was defined as nullable it would orphan the child rather than deleting it.

Read this article (Deleting Foreign Key Relationships in EF4) to get an idea of how EF handles deleting child entities. In your case you fall under case number 1.

As far as the error you're getting change your foreach to use the ForEach method off the List<T> class:

origUserConf.ColumnConfigs.ForEach(col => ctx.ColumnConfigs.Remove(col))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜