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

后端 未结 11 1800
傲寒
傲寒 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:26

    For others that may land here, there could be a really simple fix if you're using Code-First Entity Framework and just trying to add a new required column to a table with existing data.

    Note: Before you do this, just know that you will need to add a valid value to this new column for all existing rows. Think about what value you will add to the existing rows before proceeding. You may want to provide a value that indicates "Invalid" or "Unknown" to the required lookup table.

    In your model, set the column to nullable by adding the ? after the int. Save and compile the model. Run Update-Database.

    Example:

    [ForeignKey("Title")]
    public int? TitleId { get; set; }

    In the db, update the table by replacing the NULL values with a valid lookup value. In my case, I added "Unknown" to my Title lookup table and then bulk edited all rows to reference that lookup Id.

    Back in your model, remove the '?' so the column is no longer nullable. Save and compile the model and then run Update-Database

    You should now be good to go.

    0 讨论(0)
  • 2021-02-01 01:31

    This error can also happen if you have orphan records in the child table. Clean up the database should resolve the issue.

    0 讨论(0)
  • 2021-02-01 01:31

    I also received this message. The problem was that the test data in the db was incorrect. I didn't have validation on when I was testing and hence i had null values for IDs for fields that do not allow nulls. Once I fixed the data, the update-database command ran successfully.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 01:36

    This error is telling you that you are violating the foreign key constraint. To resolve you have a few solutions

    1. Fix your data - Somewhere there are records in the Clients table that have a MedicalGroupId that does not exist in the in the MedicalGroups table. Write a query to find out what IDs do not exist in the MedicalGroups table and manually fix the data yourself.
    2. Remove the foreign key constraint - Obviously if you remove the foreign key constraint you will no longer be bothered by this message. Unfortunately the database will no longer enforce this relationship and might make this problem worse in the future.
    3. Create constraint using WITH NOCHECK - You can create your foreign key constraint using the WITH NOCHECK option. This option tells SQL Server to not apply this constraint to existing data. SQL Server WILL check this constraint in any future INSERTS/UPDATES/DELETES.
    0 讨论(0)
提交回复
热议问题