简体   繁体   English

EF6代码优先:MySqlException:无法添加或更新子行:外键约束失败

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

I am using Entity Framework 6 Code First 我先使用Entity Framework 6代码

I have a property table with 2 other tables that may or may not contain further information about the property. 我有一个带有2个其他表的属性表,这些表可能包含 可能不包含有关该属性的更多信息。

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")) MySqlException:无法添加或更新子行:外键约束失败(“ efcodefirstmysql”。“ propertyforsale”,CONSTRAINT“ FK_PropertyForSale_PropertyForSale_Predictions_pid” FOREIGN KEY(“ pid”)参考“ 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? 我无法弄清楚如何指定外键,以便可以添加属性数据而不添加其他2个表中的数据?

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. 如果您不打算进行延迟加载,则可能要摆脱虚拟属性,然后对那些对象使用显式ID,就可以轻松跟踪和更新相关对象。 This approach will allow you to insert the PropertyForSale with out worrying about whether you have matching Predictions and Ratios. 这种方法将使您可以插入PropertyForSale,而不必担心是否具有匹配的预测和比率。

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(): 如果您有兴趣查看正在使用的查询,可以在执行SaveChanges()之前,在using块内添加以下内容,以使用数据库日志将其输出到控制台窗口:

db.Database.Log = Console.WriteLine;

Or anywhere else you might like to view the actual queries being executed. 或者您可能想在其他任何地方查看正在执行的实际查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC 5 EF无法添加或更新子行:外键约束失败 - ASP.NET MVC 5 EF Cannot add or update a child row: a foreign key constraint fails “无法添加或更新子行:外键约束失败”错误 - 'Cannot add or update a child row: a foreign key constraint fails' Error 错误-无法添加或更新子行:外键约束失败 - Error - Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails C#:在数据库中添加对象时,出现“ MySqlException:无法添加或更新子行:外键约束失败” - C#: I'm getting a “MySqlException: Cannot add or update a child row: a foreign key constraint fails” when i am Adding an object in my database EF6代码优先插入外键约束失败 - EF6 code-first insert foreign key constraint fail ASP.NET:无法添加或更新子行:外键约束失败 - ASP.NET: Cannot add or update child row: a foreign key constraint fails C# - 无法添加或更新子行:外键约束失败 - C# - Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:在 foreach 中执行 INSERT 时外键约束失败 - Cannot add or update a child row: a foreign key constraint fails while doing INSERT in a foreach 无法删除或更新父行:外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM