简体   繁体   English

Insert语句与外键约束冲突。 实体框架错误

[英]The Insert statement conflict with the foreign key constraint. Entity Framework error

I'm trying to create an one to many relation, where a TypeOfImmobile can have one Immobile or more. 我正在尝试创建一对多关系,其中TypeOfImmobile可以具有一个或多个Immobile。 Here's my model classes : 这是我的模型课:

public class TypeOfImmobile
{
    public int Id { get; set; }
    [Display(Name = "Type Of Immobile")]
    public string Designation { get; set; }

    public ICollection<Immobile> Immobiles { get; set; }
}

public class Immobile
{
    public int Id { get; set; }
    public int TypeOfImmobileId { get; set; }
    public virtual TypeOfImmobile TypeOfImmobile { get; set; }
}

Error occurred when I create an Immobile: 创建固定装置时发生错误:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Immobiles_dbo.TypeOfImmobiles_TypeOfImmobilesId". INSERT语句与FOREIGN KEY约束“ FK_dbo.Immobiles_dbo.TypeOfImmobiles_TypeOfImmobilesId”冲突。 The conflict occurred in database "aspnet-ARQSI-IT2-20151031052056", table "dbo.TypeOfImmobiles", column 'Id'. 数据库“ aspnet-ARQSI-IT2-2015103105205​​6”的表“ dbo.TypeOfImmobiles”的列“ Id”中发生了冲突。 The statement has been terminated. 该语句已终止。

Type of your TypeOfImmobileId property is int , so you can't set it as null . TypeOfImmobileId属性的类型为int ,因此不能将其设置为null Even you don't assign a value to an integer , it would be equal to 0 not null . 即使您不给integer赋值,它也将等于0而不是null If you didn't assign a value for TypeOfImmobileId when your create command was sent to SQL, SQL will take it as 0 in stored procedure that create the record. 如果在将创建命令发送到SQL时未为TypeOfImmobileId分配值,则SQL将在创建记录的存储过程中将其设为0 And probably TypeOfImmobile table doesn't contain a record with a 0 value in its primary key. 并且TypeOfImmobile表可能不包含主键中值为0的记录。 In other words, value of foreign key of the data you sent is invalid. 换句话说,您发送的数据的外键值无效。 It doesn't correspond any primary key value in TypeOfImmobile table. 它与TypeOfImmobile表中的任何主键值都不对应。 Your primary key should not be nullable, but foreign key should be nullable. 您的主键不应为空,但外键应为可空。

You should change type of TypeOfImmobileId from int to Nullable<int> or simply int? 您应该将TypeOfImmobileId类型从int更改为Nullable<int>或简单地更改为int? .

So TypeOfImmobileId property should be like this: 所以TypeOfImmobileId属性应该像这样:

public int? TypeOfImmobileId { get; set; }

You don't need to add ForeignKey attribute to TypeOfImmobileId property because you follow Code-First conventions and name of Foreign Key matches with name of Primary Key of related entity 您不需要将ForeignKey属性添加到TypeOfImmobileId属性,因为您遵循Code-First约定并且外键名称与相关实体的主键名称匹配

You can observe the problem as running SQL Server Profiler. 您可以在运行SQL Server Profiler时观察到该问题。 When application runs the command that will create the record, related stored procedure will be listed. 当应用程序运行将创建记录的命令时,将列出相关的存储过程。

似乎在创建Immobile您可能未将TypeOfImmobileId属性设置为有效值,即现有TypeOfImmobileId

this causes by one of following reasons: 这是由以下原因之一引起的:

1-You don't set navigation property (TypeOfImmobile) or TypeOfImmobileId 1-您未设置导航属性(TypeOfImmobile)或TypeOfImmobileId

2-The value of TypeOfImmobileId or TypeOfImmobile is not exist in database 2-数据库中不存在TypeOfImmobileId或TypeOfImmobile的值

3-Maybe you are saving Immobile before saving TypeOfImmobile 3-也许您在保存TypeOfImmobile之前先保存了Immobile

您需要通过[ForeignKey("Your_FK_ImmobileId")]在新的属性ID Your_FK_ImmobileId中设置FK

暂无
暂无

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

相关问题 INSERT 语句与 FOREIGN KEY 约束冲突。 我的实体框架配置是否适合所需的行为? - The INSERT statement conflicted with the FOREIGN KEY constraint. Is My Entity Framework Configuration good for desired behaviour? 实体框架核心 inheritance - 插入时的外键约束冲突 - Entity framework core inheritance - foreign key constraint conflict upon insert 实体框架播种导致插入语句与外键冲突 - Entity Framework seeding causes insert statement conflict with foreign key 实体框架代码第一个INSERT语句与FOREIGN KEY约束冲突 - Entity Framework code first INSERT statement conflicted with the FOREIGN KEY constraint INSERT 语句首先与实体框架数据库中的 FOREIGN KEY 约束冲突 - The INSERT statement conflicted with the FOREIGN KEY constraint in Entity Framework database first INSERT语句与FOREIGN KEY约束冲突-Entity Framework - The INSERT statement conflicted with the FOREIGN KEY constraint - Entity Framework INSERT语句与实体框架中的FOREIGN KEY约束冲突 - The INSERT statement conflicted with the FOREIGN KEY constraint in Entity Framework 实体框架6-&gt;外部服务-&gt;自引用外键-&gt; INSERT语句与FOREIGN KEY约束冲突 - Entity Framework 6 -> External Service -> self referencing foreign key -> The INSERT statement conflicted with the FOREIGN KEY constraint Entity Framework中的SaveChanges插入语句外键错误 - SaveChanges in Entity Framework insert statement foreign key error INSERT 语句与实体框架核心中的 FOREIGN KEY 约束冲突,单个语句中有 2 个表 - The INSERT statement conflicted with the FOREIGN KEY constraint in Entity framework core, 2 tables in single statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM