当我尝试使用带有实体框架的存储过程来编辑数据库时,会发生此异常:
System.Data.Entity.Infrastructure.DbUpdateException:'无法确定相关操作的有效顺序。依赖关系可能是由于外键约束,模型要求或商店生成的值引起的。'
这是我的代码:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
reg reg = db.regs.Find(id);
if (reg == null)
{
return HttpNotFound();
}
return View(reg);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(reg regs)
{
if (ModelState.IsValid)
{
Traning_ASPEntities1 dbcontext = new Traning_ASPEntities1();
dbcontext.Entry(regs).State = EntityState.Modified;
dbcontext.SaveChanges();
return RedirectToAction("display");
}
return View(regs);
}
reg模型类:
namespace UserReg.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class reg
{
public int userid { get; set; }
[Display(Name ="First Name")]
[Required(ErrorMessage ="Please enter your first name")]
[MaxLength(25,ErrorMessage ="should not exceed 25 characters")]
public string fname { get; set; }
[Display(Name ="Last Name")]
[MaxLength(30,ErrorMessage ="Should not exceed 30 characters")]
public string lname { get; set; }
[Display(Name ="Age")]
[Required(ErrorMessage = "Please enter your age")]
[Range(18,70,ErrorMessage ="Age must be between 18 & 70")]
public Nullable<int> age { get; set; }
[Display(Name ="Gender")]
[Required(ErrorMessage ="please specify your gender")]
public string gender { get; set; }
[Display(Name ="Email")]
[Required(ErrorMessage ="Enter your email")]
[EmailAddress(ErrorMessage ="Enter a valid email id")]
public string email { get; set; }
[Display(Name ="Phone")]
[Required(ErrorMessage = "Please enter your Phone number")]
[RegularExpression(@"^(?:(?:\+|0{0,2})91(\s*[\ -]\s*)?|[0]?)?[789]\d{9}|(\d[ -]?){10}\d$", ErrorMessage ="enter a valid phone number")]
public string phone { get; set; }
[Display(Name ="Username")]
[Required(ErrorMessage = "Enter your username")]
[MaxLength(25,ErrorMessage ="should not exceed 25 characters")]
public string uname { get; set; }
[Display(Name ="Password")]
[Required(ErrorMessage = "please enter your password")]
[MaxLength(15,ErrorMessage ="Must not exceed 15 characters")]
[MinLength(4,ErrorMessage ="must contain atleast 4 characters")]
[RegularExpression(@"^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{4,}$",ErrorMessage = "Minimum 4 characters, at least one letter, one number and one special character")]
[DataType(DataType.Password)]
public string pswd { get; set; }
[Display(Name = "Confirm Password")]
[Required(ErrorMessage = "please re-enter your password")]
[DataType(DataType.Password)]
[Compare("pswd",ErrorMessage ="Passwords do not match")]
public string cpswd { get; set; }
}
}
要更新的存储过程:
CREATE PROCEDURE [dbo].[regup]
@userid INT,
@fname VARCHAR(50),
@lname VARCHAR(50),
@age INT,
@gender VARCHAR(1),
@email VARCHAR(MAX),
@phone VARCHAR(15)
AS
BEGIN
--update reg set lname='asdf' where reg.userid=@userid
UPDATE reg
SET fname = @fname,
lname = @lname,
age = @age,
gender = @gender,
email = @email,
phone = @phone
WHERE userid = @userid
END