[HttpPost]
public ActionResult Create([Bind(Exclude = "vendor_id")]Vendor v)
{
if (ModelState.IsValid)
{
eproc_db.Vendors.Add(v);
eproc_db.SaveChanges();
return RedirectToAction("Index");
}
return View(v);
}
上面的代码给出了这个错误“当IDENTITY_INSERT设置为OFF时,无法在表'VENDOR'中为标识列插入显式值。
值为0.看着分析器。它也包括vendor_id列。如何排除它?
下面是模型..我还需要做些什么吗?
[Bind(Exclude = "vendor_id")]
public class Vendor
{
[Key]
public decimal vendor_id {get; set;}
[Required]
[Display(Name = "Name")]
public string vendor_name { get; set; }
[Required]
[Display(Name = "Address")]
public string vendor_address { get; set; }
[Required]
[Display(Name = "Tax ID")]
public string fed_tax_id { get; set; }
[Required]
[Display(Name = "Contact Name")]
public string contact_name { get; set; }
[Required]
[Display(Name = "Phone")]
public string phone { get; set; }
}
答案 0 :(得分:0)
问题是因为对于vendor_id,db列是数字的,这里是十进制的。我把它们都写成小数,错误就消失了。
我也从两个地方删除了[[Bind(Exclude =“vendor_id”)]