2012-07-29 53 views
0

我收到以下错误,当我填写我的形式,我的MVC应用程序:错误创建一个新的客户时 - MVC ADO.NET

UpdateException was unhandled by user code 

Unable to update the EntitySet 'Customer' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation. 

我的客户表有:ID(PK),名称,年龄,地址和时间戳。

形式只允许名称和地址填写(不知道为什么 - 我是新来的MVC,ADO.NET BTW)

这是我的代码:

[HttpPost] 
    public ActionResult Create(Customer customer) 
    { 
     customer.ID = 5; 
     db.Customers.AddObject(customer); 
     db.SaveChanges(); 
     return View(); 
    } 

我正在离开customer.ID = 5作为暂时的硬编码临时解决方案。

回答

0

如果ID是你的主键,为什么要更新?您应该更新其余的属性。

[HttpPost] 
public ActionResult Create(Customer customer) 
{ 
    customer.Name= "New name"; 
    db.Customers.AddObject(customer); 
    db.SaveChanges(); 
    return View(); 
} 
0

你有你的客户表主键? 当您在表上没有主键时,或者当您的实体集与数据库视图映射时,此错误起源于此。