简体   繁体   中英

EF6 Code First: MySqlException: Cannot add or update a child row: a foreign key constraint fails

I am using Entity Framework 6 Code First

I have a property table with 2 other tables that may or may not contain further information about the property.

So, in other words. I might want to add data to the property table only.

This is my table model:

public class PropertyForSale
{
    [Key]
    public int Id { get; set; }

    [Index("IX_pid", IsClustered = false, IsUnique = true, Order = 1), MaxLength(128)]
    public string pid { get; set; }
    public string Test { get; set; }

    [ForeignKey("pid")]
    public virtual PropertyForSale_Predictions PropertyForSale_Predictions { get; set; }
    [ForeignKey("pid")]
    public virtual PropertyForSale_Ratios PropertyForSale_Ratios { get; set; }
}

public class PropertyForSale_Predictions
{
    [Key, MaxLength(128)]
    public string pid { get; set; }
    public string Test { get; set; }
}

public class PropertyForSale_Ratios
{
    [Key, MaxLength(128)]
    public string pid { get; set; }
    public string Test { get; set; }
}

Which, visually looks like this:

在此处输入图片说明

When I attempt to add information to the property table with this code:

using (Model1 db = new Model1())
{
    db.PropertyForSale.Add(new Model.PropertyForSale
    {
        pid = "123",
        Test = "Test"
    });
    db.SaveChanges();
}

I get this error:

MySqlException: Cannot add or update a child row: a foreign key constraint fails ("efcodefirstmysql"."propertyforsale", CONSTRAINT "FK_PropertyForSale_PropertyForSale_Predictions_pid" FOREIGN KEY ("pid") REFERENCES "propertyforsale_predictions" ("pid"))

I cannot work out how to specify the Foreign Key so that I can add property data without data in the other 2 tables?

Why are you using navigation properties? If you are not planning on Lazy Loading you might want to get rid of the virtual properties, then using explicit ID's for those objects, you can easily track and update the related objects. This approach will allow you to insert the PropertyForSale with out worrying about whether you have matching Predictions and Ratios.

public class PropertyForSale
{
    [Key]
    public int Id { get; set; }

    [Index("IX_pid", IsClustered = false, IsUnique = true, Order = 1), MaxLength(128)]
    public string pid { get; set; }

    public string Test { get; set; }

    public int PropertyForSale_PredictionsId {get;set;}
    public PropertyForSale_Predictions PropertyForSale_Predictions { get; set; }

    public int PropertyForSale_RatiosId {get; set;}
    public PropertyForSale_Ratios PropertyForSale_Ratios { get; set; }
}

public class PropertyForSale_Predictions
{
    [Key, MaxLength(128)]
    public string pid { get; set; }

    public string Test { get; set; }

}

public class PropertyForSale_Ratios
{
    [Key, MaxLength(128)]
    public string pid { get; set; }

    public string Test { get; set; }

}

If you are interested in seeing the queries you are using you can use the database log to output to a console window by adding the like below inside your using block before executing SaveChanges():

db.Database.Log = Console.WriteLine;

Or anywhere else you might like to view the actual queries being executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM