Thursday 15 March 2012

c# - When I Fill the Create View and Submit It's throwing a DbEntityValidationException -



c# - When I Fill the Create View and Submit It's throwing a DbEntityValidationException -

in project trying entity framework code first apporach

table 'employee' id int not null firstname nvarchar(50) not null lastname nvarchar(50) not null middlename nvarchar(50) null address nvarchar(300) null salary decimal(16,2) null email nvarchar(50) null

it's done. added entity framework, added employeecontroller. 2 action method index & create corresponding views. far perfect.

![enter image description here][1] ![enter image description here][2]

then did next employee add: [httpget] public actionresult create() { homecoming view(); } [httppost] public actionresult create(employee emp) { if (modelstate.isvalid) { db.employees.add(emp); db.savechanges(); homecoming redirecttoaction("index"); } homecoming view(emp); }

when fill create view , submit it's throwing dbentityvalidationexception

![enter image description here][3]

dbentityvalidationexception unhandled user code exception of type 'system.data.entity.validation.dbentityvalidationexception' occurred in entityframework.dll not handled in user code additional information: validation failed 1 or more entities. see 'entityvalidationerrors' property more details.

my database table not updating too, row created there.

model definition

public partial class employee { public int id { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string middlename { get; set; } public string address { get; set; } public nullable<decimal> salary { get; set; } public string email { get; set; } }

taken here

add code post method, should help understand errors in output window

[httppost] public actionresult create(employee emp) { seek { if (modelstate.isvalid) { db.employees.add(emp); db.savechanges(); homecoming redirecttoaction("index"); } homecoming view(emp); } grab (dbentityvalidationexception e) { foreach (var eve in e.entityvalidationerrors) { system.diagnostics.debug.writeline("entity of type \"{0}\" in state \"{1}\" has next validation errors:", eve.entry.entity.gettype().name, eve.entry.state); foreach (var ve in eve.validationerrors) { system.diagnostics.debug.writeline("- property: \"{0}\", error: \"{1}\"", ve.propertyname, ve.errormessage); } } throw; } }

c# asp.net-mvc entity-framework exception database-first

No comments:

Post a Comment