The relationship could not be changed because one or more of the foreign-key properties is non-nullable

前端 未结 20 1358
情书的邮戳
情书的邮戳 2020-11-22 04:44

I am getting this error when I GetById() on an entity and then set the collection of child entities to my new list which comes from the MVC view.

The

20条回答
  •  甜味超标
    2020-11-22 05:30

    I also solved my problem with Mosh's answer and I thought PeterB's answer was a bit of since it used an enum as foreign key. Remember that you will need to add a new migration after adding this code.

    I can also recommend this blog post for other solutions:

    http://www.kianryan.co.uk/2013/03/orphaned-child/

    Code:

    public class Child
    {
        [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
    
        public string Heading { get; set; }
        //Add other properties here.
    
        [Key, Column(Order = 1)]
        public int ParentId { get; set; }
    
        public virtual Parent Parent { get; set; }
    }
    

提交回复
热议问题