【发布时间】:2021-07-21 23:51:02
【问题描述】:
当我想更改客户端输入的数据库中的值时出现异常。
这是我数据库https://ibb.co/q1cwwYq 中的所有客户,当我单击一个客户时,我将客户重定向到编辑视图,看起来像这样https://ibb.co/NSfvnGg。现在,当我单击保存按钮时,我收到错误 System.Data.Entity.Infrastructure.DbUpdateException: 'An error occurred while updating the entries. See the inner exception for details.'
SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Customers_dbo.MemberShipTypes_MemberShipTypeID". The conflict occurred in database "Vidly.Models.ApplicationDbContext", table "dbo.MemberShipTypes", column 'Id'.
The statement has been terminated.
这是我的客户表格
@model Vidly.Models.NewCustomerViewModel
@{
ViewBag.Title = "New";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>New Customer</h2>
@using (Html.BeginForm("Save", "Customer"))
{
<div class="form-group">
@Html.LabelFor(m => m.Customer.Name)
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.Birthdate)
@Html.TextBoxFor(m => m.Customer.Birthdate,"{0:d MMM yyyy}", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Customer.MemberShipTypeID)
@Html.DropDownListFor(m => m.Customer.MemberShipType,new SelectList(Model.MemberShipTypes,"ID","Name"),"", new { @class = "form-control" })
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsLetter) Subscribed to Newsletter?
</label>
</div>
@Html.HiddenFor(m => m.Customer.Id);
<button type="submit" class="btn btn-primary">Save</button>
}
在 CustomerController 中保存操作
public ActionResult Save(Customer customer)
{
if(customer.Id == 0)
_context.Customers.Add(customer);
else
{
var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);
customerInDb.Name = customer.Name;
customerInDb.Birthdate = customer.Birthdate;
customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
customerInDb.MemberShipTypeID = customer.MemberShipTypeID;
}
_context.SaveChanges();
return RedirectToAction("Customers", "Customer");
}
客户类别
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Vidly.Models
{
public class Customer
{
public int Id { get; set; }
[Required] [StringLength(255)]
public string Name { get; set; }
public bool IsSubscribedToNewsLetter { get; set; }
public MemberShipType MemberShipType { get; set; }
[Display(Name = "Membership Type")]
public byte MemberShipTypeID { get; set; }
[Display(Name = "Date of Birth")]
public DateTime? Birthdate { get; set; }
}
}
我在代码的_context.SaveChanges(); 这一行遇到错误。
【问题讨论】:
-
你有什么错误?
-
发布完整的异常文本,而不仅仅是消息的一部分。您可以使用
Exception.ToString()或在调试时单击异常弹出窗口中的Copy Details轻松获取全文。正如消息所说:See the inner exception for details. -
@PanagiotisKanavos
SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Customers_dbo.MemberShipTypes_MemberShipTypeID". The conflict occurred in database "Vidly.Models.ApplicationDbContext", table "dbo.MemberShipTypes", column 'Id'. The statement has been terminated. -
该错误表明
MembershipTypeID值无效。该代码试图存储MembershipTypes表中不存在的值 -
@PanagiotisKanavos 好的,我更改了
customerInDb.MemberShipTypeID = customer.MemberShipTypeID,但现在我收到了Vidly.Models.Customer.MemberShipType.get returned null.System.NullReferenceException: 'Object reference not set to an instance of an object.'
标签: c# asp.net-mvc database entity-framework