Entity Framework The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

后端 未结 11 1827
傲寒
傲寒 2021-02-01 00:34

On updating database in Entity Framework , Code first Migration, I am getting this error:

The ALTER TABLE statement conflicted with the FOREIGN KEY constr

11条回答
  •  悲&欢浪女
    2021-02-01 01:35

    this problem appear because your table is not empty. so you shoud add the new field without attaching it like foreign key. public int? MedicalGroupId { get; set; }.And execute update-database command in package manage console. Then fill the field in this table(client) with the right data (value exists in MedicalGroupsId). Insert the line to create the foreign key

    [ForeignKey("MedicalGroupId")]
    public virtual MedicalGroups MedicalGroup { get { return _MedicalGroup; } set { _MedicalGroup = value; } }
    

    in the end execute the update-database command. It will be ok.

提交回复
热议问题