Sql server ASP.NET核心SqlException:insert语句与外键约束冲突
我在我的应用程序(ASP.NET核心应用程序)中创建了两个具有一对多关系的表(Customer,Sale),其中一个客户有多张销售发票,但在创建销售时,我收到了错误消息 处理请求时发生未处理的异常。Sql server ASP.NET核心SqlException:insert语句与外键约束冲突,sql-server,entity-framework,asp.net-core,Sql Server,Entity Framework,Asp.net Core,我在我的应用程序(ASP.NET核心应用程序)中创建了两个具有一对多关系的表(Customer,Sale),其中一个客户有多张销售发票,但在创建销售时,我收到了错误消息 处理请求时发生未处理的异常。 SqlException:INSERT语句与外键约束“FK\u Sale\u Customer\u customerId”冲突。冲突发生在数据库“LibraryBookManagementDatabase”、表“dbo.Customer”、列“Customerid”中。声明已终止 这是客户车型类别
SqlException:INSERT语句与外键约束“FK\u Sale\u Customer\u customerId”冲突。冲突发生在数据库“LibraryBookManagementDatabase”、表“dbo.Customer”、列“Customerid”中。声明已终止 这是
客户
车型类别:
public class Customer
{
[Key]
public int Customerid { get; set; }
[Required, MinLength(2), MaxLength(50)]
public string Name { get; set; }
public double NationalID { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public ICollection<Sale> Sales { get; set; }
}
public class Sale
{
[Key]
public int id { get; set; }
[Required]
[DisplayName("Sales Date")]
public DateTime SalesDate { get; set; }
[DisplayName("Sales Code")]
public string SaleCode { get; set; }
[ForeignKey("Customerid")]
public int customerId { get; set; }
public Customer Customer { get; set; }
public ICollection<Book> Books { get; set; }
}
这是创建新销售的页面
<form asp-controller="Sale" asp-action="Create" method="post" class="form-horizontal" role="form">
<div class="col-md-12">
<div class="form-inline">
<div class="col-md-4">
<div class="form-group">
<label for="Name">Customer   </label>
<select class="form-control border-input" asp-items="ViewBag.Customerid" placeholder = "Customer"></select>
</div>
</div>
</div>
</div>
</form>
Sale
使用以下方法存储:
public void Create(sale entity)
{
_context.Add(entity);
_context.SaveChanges();
}
有人能帮我吗?根据您的代码,我已经创建了一个示例,似乎在插入新的销售实体时,如果customerId为空,它将显示“insert语句与外键约束冲突”错误。您可以在createhttppost方法中设置断点来验证它
这个问题与select元素有关,因为它不包含name属性,所以在提交表单时,customer为null(“0”是默认值,在我的数据库中,customer是从1开始的,不包含“0”)
要解决此问题,请尝试按如下方式修改“选择元素代码”:
<select asp-for="customerId" class ="form-control" asp-items="@ViewBag.Customerid" placeholder = "Customer"></select>
或
结果(成功):
编辑
此外,关于ForeignKey属性,我们可以如下使用:
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
[ForeignKey("Standard")]
public int StandardRefId { get; set; }
public Standard Standard { get; set; }
}
public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; }
public ICollection<Student> Students { get; set; }
}
公共班级学生
{
公共int StudentID{get;set;}
公共字符串StudentName{get;set;}
[外键(“标准”)]
公共int-standarrefid{get;set;}
公共标准{get;set;}
}
公共类标准
{
公共int标准ID{get;set;}
公共字符串StandardName{get;set;}
公共ICollection学生{get;set;}
}
在上面的示例中,[ForeignKey]属性应用于StandardRefId,并在导航属性Standard的名称中指定。这将在Students表中创建名为StandardRefId的外键列,防止在数据库中生成StandardId列
公共班级学生
{
公共int StudentID{get;set;}
公共字符串StudentName{get;set;}
公共int-standarrefid{get;set;}
[ForeignKey(“StandardRefId”)]
公共标准{get;set;}
}
公共类标准
{
公共int标准ID{get;set;}
公共字符串StandardName{get;set;}
公共ICollection学生{get;set;}
}
在上面的示例中,[ForeignKey]属性应用于标准导航属性,并指定外键属性StandardRefId的名称。这将在Students表中创建名为StandardRefId的外键列,防止在数据库中生成StandardId列
更多详细信息,请参阅:非常简单:您试图插入一个
销售
,其中包含客户ID
,该ID在客户
表中不存在->这就是外键约束的全部意义-避免这种情况!因此,请解决此问题-确保仅在Sale
中使用Customer
表中存在的CustomerId
。-数据库设计101@marc_s您的意思是将Customerid更改为Customerid吗?@marc_s您的意思是将Customerid转换为Customerid吗?我的意思是:在保存之前,调试您的代码并检查Sale
对象上的Customerid
的值。确保CustomerId
表中已经存在一个具有此值的客户,以避免此错误。您需要在运行时在调用的Create
方法中进行检查-model.CustomerId的值是多少??数据库中是否已经存在具有该CustomerId
的客户?您收到的错误消息似乎表明情况并非如此-那么,您为什么试图用无效/不存在的CustomerId
保存Sale
对象呢??这就是你需要弄明白的。。。。
<select asp-for="customerId" class ="form-control" asp-items="@ViewBag.Customerid" placeholder = "Customer"></select>
<select name="customerId" class ="form-control" asp-items="@ViewBag.Customerid" placeholder = "Customer"></select>
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
[ForeignKey("Standard")]
public int StandardRefId { get; set; }
public Standard Standard { get; set; }
}
public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; }
public ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StandardRefId { get; set; }
[ForeignKey("StandardRefId")]
public Standard Standard { get; set; }
}
public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; }
public ICollection<Student> Students { get; set; }
}