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

C#Sql.NetSql ServerEntity Framework

C# Problem Overview


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

> The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Clients_dbo.MedicalGroups_MedicalGroupId". The conflict occurred in database "hrbc", table "dbo.MedicalGroups", column 'Id'.

This is my class:

public partial class Client
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

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

Here is my 2nd class:

public partial class MedicalGroups
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

And this is my migration which I am trying to apply:

public override void Up()
{
    AddForeignKey("dbo.Clients", "MedicalGroupId", "dbo.MedicalGroups", "Id");
    CreateIndex("dbo.Clients", "MedicalGroupId");
}

C# Solutions


Solution 1 - C#

Check that there is not existing data in the database that is conflicting with the FK constraint causing the creation to fail.

Solution 2 - C#

I think @Cory was getting you close to the correct solution, you just did not take the time to investigate.

In add-migration code, the migration probably generated

public override void Up()
{
AddColumn("dbo.ClientContacts", "FamilialRelationshipId", c => c.Int(nullable: false));
CreateIndex("dbo.ClientContacts", "FamilialRelationshipId");
AddForeignKey("dbo.ClientContacts", "FamilialRelationshipId", "dbo.FamilialRelationships",        "FamilialRelationshipId");
}

Notice nullable:false; If your Model had an Id of int instead of int? (nullable int) the migration code will set nullable to false. Your model shows you are using a non-nullable int which defaults to 0, and you probably don't have a foreign key item with a value of 0.

Now, you will have to either create a default value, that exists in the foreign key table, or Create the Constraint with no check if you are using SQL Server to create the constraint. Remember this though: If you decorate your property with a [DefaultValue(0)] attribute, it won't change the existing data, like a SQL Column Add would, if a default value was specified.

I recommend you change your Model Class to allow a nullable int. Then, in your seed method, create a simple method against dbcontext to update the new column with a default value, because the [DefaultValue] attribute in data annotations will not modify your data.

Add-Migration / Update-Database to create the column and constraint. Next, modify your model if you desire to allow for a non-nullable int, and presuming you changed all rows to some valid value for the foreign key, Add-Migration / Update-database again. This gives you an unbroken chain in your model migration. Will come in handy later when you publish to a live site, because the flow of your data model changes will be intact.

  • Hope this helps.

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

I got the solution of my Problem. Problem is "data" which i have in my clients table. Because my client table have medicalgroupid values which are not actually exist that's why it is giving me error on foreign key constraint.

Update Client set MedicalGroupId = NULL

Solution 7 - C#

I had this issue as well with defaultValue set, gave:

"The object is dependent on column ALTER TABLE ALTER COLUMN failed because one or more objects access this column".

Ended up move the AddForeignKey/DropForeignKey to a new migration and ran them in separate update-database commands (dont excecute both migrations in one command then it failed for me.)

    public override void Up()
    {
        CreateTable(
            "dbo.DecisionAccesses",
            c => new
            {
                Id = c.Int(nullable: false),
                Name = c.String(nullable: false, maxLength: 50),
            })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "dbo.DecisionPersonStatus",
            c => new
            {
                Id = c.Int(nullable: false),
                Name = c.String(nullable: false, maxLength: 50),
            })
            .PrimaryKey(t => t.Id);

        AddColumn("dbo.DecisionForm_DecisionFields", "DecisionAccessId", c => c.Int(nullable: false, defaultValue: (int)DecisionAccess.Creator));
        AddColumn("dbo.DecisionMatterPersons", "DecisionPersonStatusId", c => c.Int(nullable: false, defaultValue: (int)DecisionAccess.Creator));
        CreateIndex("dbo.DecisionForm_DecisionFields", "DecisionAccessId");
        CreateIndex("dbo.DecisionMatterPersons", "DecisionPersonStatusId");
        //I moved outcommented to next migration and ran the migrations in separate steps to avoid: (The object is dependent on column ALTER TABLE ALTER COLUMN failed because one or more objects access this column)
        //AddForeignKey("dbo.DecisionForm_DecisionFields", "DecisionAccessId", "dbo.DecisionAccesses", "Id", cascadeDelete: true); 
        //AddForeignKey("dbo.DecisionMatterPersons", "DecisionPersonStatusId", "dbo.DecisionPersonStatus", "Id", cascadeDelete: true);
    }


    public override void Down()
    {
        //Moved to next migration
        //DropForeignKey("dbo.DecisionMatterPersons", "DecisionPersonStatusId", "dbo.DecisionPersonStatus");
        //DropForeignKey("dbo.DecisionForm_DecisionFields", "DecisionAccessId", "dbo.DecisionAccesses");
    }

Solution 8 - C#

Assuming your migrations are in correct order i.e. table associated with Foreign key will get created before the reference. Follow the following steps:

  1. Backup current database from SQL management studio (If required)
  2. Delete database from SQL management studio
  3. Type 'Update-Database'in Visual Studio 'Package Manager Console'

Solution 9 - C#

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

Solution 10 - C#

Run the Add-Migration InitialCreate –IgnoreChanges command in Package Manager Console. This creates an empty migration with the current model as a snapshot.

Run the Update-Database command in Package Manager Console. This will apply the InitialCreate migration to the database. Since the actual migration doesn’t contain any changes, it will simply add a row to the __MigrationsHistory table indicating that this migration has already been applied.

You can get more detail here : https://msdn.microsoft.com/en-us/library/dn579398(v=vs.113).aspx

Solution 11 - C#

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.

Solution 12 - C#

Had that issue but fortunately I was just building my tables. I only had a couple sample items in each table. Deleted all the rows in both tables then it worked. I'm a noob so take it for what it's worth

Solution 13 - C#

You could also use data seeding via

modelBuilder.Entity<MedicalGroups>()
.HasData(new MedicalGroups { ... }, new MedicalGroups { ... })

in your DbContexts class OnModelCreating method.

That way you can insert all the MedicalData rows whos Id is missing from the referencing table. At least in my case the data got seeded bofere it was needed and update-database worked properly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZaid IqbalView Question on Stackoverflow
Solution 1 - C#TurnkeyView Answer on Stackoverflow
Solution 2 - C#RickIsWrightView Answer on Stackoverflow
Solution 3 - C#CoryView Answer on Stackoverflow
Solution 4 - C#chekibView Answer on Stackoverflow
Solution 5 - C#GrayDwarfView Answer on Stackoverflow
Solution 6 - C#Zaid IqbalView Answer on Stackoverflow
Solution 7 - C#SgeddaView Answer on Stackoverflow
Solution 8 - C#Kuldeep PadhiarView Answer on Stackoverflow
Solution 9 - C#Christopher Cabezudo RodriguezView Answer on Stackoverflow
Solution 10 - C#ErsinView Answer on Stackoverflow
Solution 11 - C#Fabian MaduraiView Answer on Stackoverflow
Solution 12 - C#TimView Answer on Stackoverflow
Solution 13 - C#Jens ManderView Answer on Stackoverflow