[英]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-20151031052056”的表“ 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
属性设置为有效值,即现有TypeOfImmobile
的Id
。
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.