c# - DbEntityValidationException When Inserting Record -
sorry long post. have these table relationships:
- room has many-to-many relationship activity - roomactivity has one-to-many relationship room , activity - item has many-to-many relationship part - itempart has one-to-many relationship item , part - stage has foreign key relationship roomactivity , itempart - submission has foreign key relationship stage
i have web application employees can submit today, consist of room, activity, item, part submit form.
my controller:
public actionresult create() { viewbag.activityrejectcodeid = getrejectcodesbyactivity(0); viewbag.activities = getactivities(); viewbag.workstations = getrooms(); viewbag.platforms = getitems(); viewbag.parts = getparts(); return view(); } [httppost] [validateantiforgerytoken] public actionresult create(workordersubmission workordersubmission) { if (modelstate.isvalid) { var activityid = int32.parse(request.form["stage.roomactivity.activity.id"]); var workstationid = int32.parse(request.form["stage.roomactivity.workstation.id"]); var platformid = int32.parse(request.form["stage.itempart.platform.id"]); var partid = int32.parse(request.form["stage.itempart.part.id"]); var rs = (from ps in db.stages join wa in db.roomactivities on ps.roomactivityid equals wa.id join pp in db.itemparts on ps.itempartid equals pp.id ps.roomactivity.activityid == activityid && ps.roomactivity.roomid == workstationid && ps.itempart.itemid == platformid && ps.itempart.itemid == partid select new { ps.id }).firstordefault(); var stageid = rs.id; workordersubmission.stageid = stageid; workordersubmission.submissiondate = datetime.now; // error when saving here db.workordersubmissions.add(workordersubmission); db.savechanges(); return redirecttoaction("index"); } viewbag.stageid = new selectlist(db.stages.orderby(p => p.name), "id", "name", workordersubmission.stageid); return view(workordersubmission); } public selectlist getactivities() { var results = (from ps in db.stages join wa in db.roomactivities on ps.roomactivityid equals wa.id join in db.activities on wa.activityid equals a.id select new { id = wa.activityid, name = a.name }) .distinct() .orderby(n => n.name); return new selectlist(results, "id", "name"); }
my view:
<div class="editor-label"> @html.labelfor(model => model.stage.roomactivity.activity.id, "activity") </div> <div class="editor-field"> @html.dropdownlistfor(model => model.stage.roomactivity.activity.id, (selectlist)viewbag.activities, "") @html.validationmessagefor(model => model.stage.roomactivity.activity.id) </div>
i'm getting error below(updated):
validation failed entity [part]. validation errors: number: number field required. validation failed entity [item]. validation errors: name: name field required. validation failed entity [activity]. validation errors: name: name field required. validation failed entity [room]. validation errors: name: name field required
why error on part, item, activity, room? i'm trying insert new submission.
stage model:
public partial class stage { public stage() { this.workordersubmissions = new hashset<workordersubmission>(); } public int id { get; set; } public int roomactivityid { get; set; } public int itempartid { get; set; } public string description { get; set; } public string name { get; set; } public virtual itempart itempart { get; set; } public virtual roomactivity roomactivity { get; set; } public virtual icollection<workordersubmission> workordersubmissions { get; set; } }
workordersubmission model:
public partial class workordersubmission { public int id { get; set; } public int workorderid { get; set; } public int stageid { get; set; } public system.datetime submissiondate { get; set; } public virtual stage stage { get; set; } public virtual workorder workorder { get; set; } }
could add code more information dbentityvalidationexception , show it?
try { db.savechanges(); } catch (dbentityvalidationexception e) { string errorformat = @"validation failed entity [{0}]. validation errors:" + environment.newline + @"{1}"; var errorlist = new list<string>(); foreach (var entityvalidationerror in e.entityvalidationerrors) { var entityname = entityvalidationerror.entry.entity.gettype().name; var errors = string.join(environment.newline, entityvalidationerror.validationerrors.select(a => a.propertyname + ": " + a.errormessage)); errorlist.add(string.format(errorformat, entityname, errors)); } throw new exception(string.join(environment.newline + environment.newline, errorlist) + environment.newline, e); }
Comments
Post a Comment