簡體   English   中英

使用EF模型在MVC中創建/更新不起作用

[英]Create/update in mvc using EF model not working

我在MVC應用程序中使用現有的sql數據庫。 對於其中一張表,創建/更新功能不起作用。 我以為是因為我的應用程序無法檢索SQL中定義的自動生成的ID,因此將null值插入不可為null的字段中,從而導致應用程序損壞。 因此,我的問題是如何檢索在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> 

因此,我的問題是如何檢索在sql數據庫中定義的自動生成的字段以顯示在MVC5應用程序中。

遵循步驟

  • 右鍵單擊EDMX設計器的設計器表面,然后單擊Update Model From Database ...”。

默認情況下會刷新所有實體,只有在選擇新實體后才添加它們。

編輯:如果它不是很好刷新。 在EDMX設計器中選擇所有表和視圖。 刪除它們。 然后,從數據庫更新模型

  • Model1.tt單擊鼠標右鍵,然后選擇“ Run Custom Tool ”,然后保存並立即構建,請參見生成類。 Model1.Context.tt單擊鼠標右鍵,然后選擇“ Run Custom Tool ”,然后保存並立即構建,請參見屬性IN上下文類的生成,例如

PS

還請閱讀此鏈接,它非常有用: http : //blog.jongallant.com/2012/08/entity-framework-manual-update/

您應該將CustomerID作為參數傳遞給Controller。 請勿授予客戶ID空值的權限,並使其設為AUTO_INCREMENT

<div class="form-group">
    @Html.TextBoxFor(model => model.CustomerID, new {style = 'display:none'})
    @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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM