這是我試圖將數據保存到我的表時得到的錯誤。我在MVC中創建了腳手架。填寫表單中的數據後,當我點擊創建按鈕,發生下面的錯誤
錯誤:
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Unable to update the
EntitySet
'CustormerD
' because it has aDefiningQuery
and no<InsertFunction>
element exists in the<ModificationFunctionMapping>
element to support the current operation.
控制器代碼:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="custid,name,cell_No,address,date,time,serviceNo")] CustormerD custormerd)
{
if (ModelState.IsValid)
{
db.CustormerDs.Add(custormerd);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(custormerd);
}
見下表代碼:
public partial class CustormerD
{
public int custid { get; set; }
public string name { get; set; }
public Nullable<int> cell_No { get; set; }
public string address { get; set; }
public Nullable<System.DateTime> date { get; set; }
public Nullable<System.TimeSpan> time { get; set; }
public Nullable<int> serviceNo { get; set; }
}
SQL代碼
CREATE TABLE [dbo].[CustormerD](
[custid] [int] IDENTITY(1,1) NOT NULL,
[name] [nchar](20) NULL,
[cell_No] [int] NULL,
[address] [nchar](20) NULL,
[date] [date] NULL,
[time] [time](7) NULL,
[serviceNo] [int] NULL
) ON [PRIMARY]
如果您的表沒有主鍵,則可能會發生此錯誤! –
在'custid'上添加'[Key]' –
可否請我協助我在我的數據庫表中失敗的地方 – sibonile