C# 先使用EF数据库保存到数据库-默认值问题
我是MVC和EF新手,在更新SQL数据库中的一行时遇到问题,该行的列上设置了默认值 我在属性(edmx)上将“StoredGeneratedPattern”设置为“Computed”,以便在通过EF DB 1st插入新记录时调用数据库中的默认值。这很好 但是,当我将此属性设置为“computed”的对象更新时,数据库中的默认值仍在设置中。我假设这个属性现在有一个值并且不为null,所以不会调用默认值 我已经通过SSMS直接更新了记录,这一切都如期进行 我最初确实通过person模型中的代码设置了一个默认值,但当我刷新edmx时,该代码被删除,这就是我使用StoredGeneratedPattern的原因 如果有人能就如何纠正这个问题给我建议,我将不胜感激。C# 先使用EF数据库保存到数据库-默认值问题,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我是MVC和EF新手,在更新SQL数据库中的一行时遇到问题,该行的列上设置了默认值 我在属性(edmx)上将“StoredGeneratedPattern”设置为“Computed”,以便在通过EF DB 1st插入新记录时调用数据库中的默认值。这很好 但是,当我将此属性设置为“computed”的对象更新时,数据库中的默认值仍在设置中。我假设这个属性现在有一个值并且不为null,所以不会调用默认值 我已经通过SSMS直接更新了记录,这一切都如期进行 我最初确实通过person模型中的代码设置了
你能不能把图片换成代码?我已经上传了编辑程序的控制器代码。LastUpdated和Archived在SQL数据库中具有默认值。Computed表示数据库中没有列,而是从select语句计算该值。这会导致该值未保存,因为EF认为没有列可保存该值。因此,对于你的需求,计算似乎是错误的选择。我不确定,但最好在模型中设置一个默认值,不设置任何属性。我最初在模型构造函数中设置了属性,但当我刷新edmx时,代码消失了。类似地,在属性上方添加数据类型时,在执行刷新时也会删除该数据类型。e、 g.[DataType(DataType.Password)]-无论如何,谢谢你的解释,这对我来说很有意义。此后,我删除了计算值,并在edmx中设置了默认属性,这似乎有效!
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "Description", person.LocationID);
ViewBag.StatusID = new SelectList(db.Status, "StatusID", "Description", person.StatusID);
ViewBag.TitleID = new SelectList(db.Titles, "TitleID", "Description", person.TitleID);
ViewBag.PersonID = new SelectList(db.Volunteers, "VolunteerID", "VolunteerID", person.PersonID);
return View(person);
}
// POST: People/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "PersonID,TitleID,Forename,MiddleName,Surname,DateofBirth,Gender,LocationID,StatusID,LastUpdated,Archived")] Person person)
{
if (ModelState.IsValid)
{
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "Description", person.LocationID);
ViewBag.StatusID = new SelectList(db.Status, "StatusID", "Description", person.StatusID);
ViewBag.TitleID = new SelectList(db.Titles, "TitleID", "Description", person.TitleID);
ViewBag.PersonID = new SelectList(db.Volunteers, "VolunteerID", "VolunteerID", person.PersonID);
return View(person);
}