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

前端 未结 20 1032
情书的邮戳
情书的邮戳 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:19

    I used Mosh's solution, but it was not obvious to me how to implement the composition key correctly in code first.

    So here is the solution:

    public class Holiday
    {
        [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int HolidayId { get; set; }
        [Key, Column(Order = 1), ForeignKey("Location")]
        public LocationEnum LocationId { get; set; }
    
        public virtual Location Location { get; set; }
    
        public DateTime Date { get; set; }
        public string Name { get; set; }
    }
    

提交回复
热议问题