c# - EF6 Updating Relative Table Values -
when user clicks button next element on page want elements isaccepted value change. when button pressed db.savechanges() command throws exception of type entityvalidationerrors.
controller
namespace webapplication6.controllers { public class appcontroller : controller { [httpget] public actionresult accept(int? id) { emailformmodel itemtoupdate = db.projectinfo.find(id); itemtoupdate.isaccepted = true; //only value want change db.savechanges(); return redirecttoaction("maindbview"); } } }
model
public class emailformmodel { public emailformmodel() { isaccepted = false; ressurs = new list<ressursbehov>() { new ressursbehov() }; } public int id { get; set; } public bool isaccepted { get; set; } [required(errormessage = "påkrevet"), display(name = "prosjektnummer"), regularexpression(@"^([pp])\d*$", errormessage = "prosjektnummer er på formen: p1234 ")] public string prosjektnummer { get; set; } [required(errormessage = "påkrevet"), display(name = "prosjektnavn")] public string prosjektnavn { get; set; } [required(errormessage = "påkrevet"), display(name = "prosjekttype")] public string prosjekttype { get; set; } [required(errormessage = "påkrevet"), display(name = "prosjektleder")] public string prosjektleder { get; set; } [required(errormessage = "påkrevet"), display(name = "skjema utfylt av")] public string prosjektregistrerer { get; set; } [required] public virtual list<ressursbehov> ressurs { get; set; } } public class ressursbehov { public int id { get; set; } [required(errormessage = "påkrevet"), display(name = "ressurstype")] public string ressurstype { get; set; } [required(errormessage = "påkrevet"), display(name = "navn på ressurs")] public string navn_på_ressurs { get; set; } [required(errormessage = "påkrevet"), display(name = "ukenummer"), regularexpression(@"^([1-9]|[1-4][0-9]|5[0-2])$", errormessage = "ugyldig verdi")] public int? ukenummer { get; set; } [required(errormessage = "påkrevet"), display(name = "antall timer"), regularexpression(@"^([1-9]|[1-9][0-9])$", errormessage = "ugyldig verdi")] public int? antall_timer { get; set; } [required(errormessage = "påkrevet"), display(name = "antall uker"), regularexpression(@"^([1-9]|[1-9][0-9])$", errormessage = "ugyldig verdi")] public int? antall_uker { get; set; } [required] public virtual emailformmodel realemailformmodel { get; set; } }
context class
public class talktodb : dbcontext { public dbset<emailformmodel> projectinfo { get; set; } public dbset<ressursbehov> projectextra { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { //one-to-many modelbuilder.entity<ressursbehov>() .hasrequired<emailformmodel>(s => s.realemailformmodel) .withmany(s => s.ressurs); } }
update
this error get:
exception thrown: 'system.data.entity.validation.dbentityvalidationexception' in entityframework.dll iisexpress.exe information: 0 : property: ressurstype error: påkrevet
iisexpress.exe information: 0 : property: navn_på_ressurs error: påkrevet
iisexpress.exe information: 0 : property: ukenummer error: påkrevet
iisexpress.exe information: 0 : property: antall_timer error: påkrevet
iisexpress.exe information: 0 : property: antall_uker error: påkrevet
that exception means entity failed validation. if debug, can inspect modelstate
object , items non-empty errors
property.
alternatively, can rewrite exception little more descriptive:
try { db.savechanges(); } catch (dbentityvalidationexception e) { var errormessages = e.entityvalidationerrors .selectmany(x => x.validationerrors) .select(x => x.errormessage); var fullerrormessage = string.join("; ", errormessages); var exceptionmessage = string.concat(e.message, " validation errors are: ", fullerrormessage); throw new dbentityvalidationexception(exceptionmessage, e.entityvalidationerrors); }
that list out each validation error part of exception message, making lot easier tell need fix. if you're using repository or service can build "save" method, happens automatically, without having use try
block every time want save entity.
Comments
Post a Comment