how to exclude identity column - mvc3
[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);
}
The Code above gives this error "Cannot insert explicit value for identity column in table 'VENDOR' when IDENTITY_INSERT is set to OFF.
the value is going in as 0. looked at profiler. it's including vendor_id column also. how to exclude it?
below is the model.. Do i have to do something more?
[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; }
}
The issue was because for vendor_id the db column was numeric and here it was decimal. I made them both decimal and the error disappeared.
Also i removed ([Bind(Exclude = "vendor_id")] from both places
精彩评论