我在MVC应用程序中使用现有的sql数据库。对于其中一个表,创建/更新功能不起作用。我假设这是因为我的应用程序无法检索SQL中定义的自动生成的ID,因此将空值插入到不可为空的字段中,从而导致应用程序中断。所以,我的问题是我如何检索我的sql数据库中定义的自动生成的字段,以显示在我的MVC5应用程序中。非常感谢任何可以提供帮助的人。
模特:
public partial class Customer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Customer()
{
this.Cards = new HashSet<Card>();
this.Stores = new HashSet<Store>();
}
public int CustomerID { get; set; }
public int DiscountLevelID { get; set; }
public int LoyaltyLevelID { get; set; }
public string CustomerCompanyName { get; set; }
public string CustomerName { get; set; }
public string CustomerSurname { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string CustomerGUID { get; set; }
public int CustomerStatus { get; set; }
public string CustomerAddress { get; set; }
public string CustomerTel { get; set; }
public string CustomerCel { get; set; }
public Nullable<int> CustomerNumber { get; set; }
public string CustomerContact { get; set; }
public string CustomerLogo { get; set; }
public string CustomerLogoPath { get; set; }
public int LastStoreCustomerSyncID { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Card> Cards { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Store> Stores { get; set; }
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerID,DiscountLevelID,LoyaltyLevelID,CustomerCompanyName,CustomerName,CustomerSurname,CustomerGUID,CustomerStatus,CustomerAddress,CustomerTel,CustomerCel,CustomerNumber,CustomerContact,CustomerLogo,CustomerLogoPath,LastStoreCustomerSyncID")] Customer customer)
{
if (ModelState.IsValid)
{
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
// GET: Companies/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
return View(customer);
}
// POST: Companies/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CustomerID,DiscountLevelID,LoyaltyLevelID,CustomerCompanyName,CustomerName,CustomerSurname,CustomerGUID,CustomerStatus,CustomerAddress,CustomerTel,CustomerCel,CustomerNumber,CustomerContact,CustomerLogo,CustomerLogoPath,LastStoreCustomerSyncID")] Customer customer)
{
if (ModelState.IsValid)
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
视图
<div class="form-group">
@Html.LabelFor(model => model.CustomerGUID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerGUID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerGUID, "", new { @class = "text-danger" })
</div>
</div>
答案 0 :(得分:0)
所以,我的问题是如何检索自动生成的字段 在我的sql数据库中定义,以在我的MVC5应用程序中显示。
按照步骤
[
{
"primary":{
"name":"ABC"
},
"Id":"1"
},
{
"primary":{
"name":"PQR"
},
"Id":"2"
},
{
"primary":{
"name":"XYZ"
},
"Id":"3"
}
]
... 默认情况下刷新所有实体,只有在您选择新实体时才会添加新实体。
编辑:如果不是很清爽。 在EDMX设计器中选择所有表和视图。 删除它们。 然后,从数据库更新模型
@Test(expected = IllegalStateException.class,timeout = 1000)
public void lattesRequireMilk() {
// given
Cafe cafe = new Cafe();
cafe.restockBeans(7);
// when
cafe.brew(Latte);
}
,然后选择“Update Model From Database
”保存并立即构建,即可生成类。
右键单击Model1.tt
并选择“Run Custom Tool
”保存并立即构建,请参阅属性IN上下文类生成,如P.S
阅读此链接,它非常有用:http://blog.jongallant.com/2012/08/entity-framework-manual-update/
答案 1 :(得分:0)
您应该将CustomerID作为参数传递给Controller。不要为CustomerID授予null值的权限,并将其设为AUTO_INCREMENT
top_view