想要将更改保存到数据库时出现异常
Posted
技术标签:
【中文标题】想要将更改保存到数据库时出现异常【英文标题】:Getting Exception when want to Save Changes to database 【发布时间】: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.'
【参考方案1】:
你有错误
@Html.DropDownListFor(m => m.Customer.MemberShipType,new SelectList(Model.MemberShipTypes,"ID","Name"),"", new @class = "form-control" )
应该是
@Html.DropDownListFor(m => m.Customer.MemberShipTypeId,new SelectList(Model.MemberShipTypes,"ID","Name"),"", new @class = "form-control" )
这就是您的 Customer.MemberShipTypeId 值无效且无法更新的原因。
也可以更新试试这个代码:
var customerInDb = _context.Customers.FirstOrDefault(c => c.Id == customer.Id);
if (customerInDb !=null)
customerInDb.Name = customer.Name;
customerInDb.Birthdate = customer.Birthdate;
customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
customerInDb.MemberShipTypeID = customer.MemberShipTypeID;
_context.Entry(CustomerInDb).State = EntityState.Modified;
....
_context.SaveChanges();
【讨论】:
对象已经处于Modified
状态,这就是为什么尝试更新而不是插入以上是关于想要将更改保存到数据库时出现异常的主要内容,如果未能解决你的问题,请参考以下文章