c# - entity framework validation context - null exception in null test -
i working on pc configurator application , have problem validating. point of app check if socket on motherboard same socket of cpu etc.
i made code first database using scaffolding in visual studio 2013.
public class configuration : ivalidatableobject { [key] public int configurationid { get; set; } [required] [stringlength(50, minimumlength = 3)] [displayname("configuration name")] public string configname { get; set; } [foreignkey("mb_id")] public motherboard motherboard { get; set; } [column("mb_id", typename = "int")] public nullable<int> mb_id { get; set; } [foreignkey("cpu_id")] public cpu cpu { get; set; } [column("cpu_id", typename = "int")] public nullable<int> cpu_id { get; set; } }
this part of configuration model using (rest of looks pretty similar - other components)
i scaffolded model controller.
this start of validation , seems problem somewhere here (at least vs says so).
public ienumerable<validationresult> validate(validationcontext validationcontext) { if (motherboard == null) { throw new argumentnullexception("motherboard"); } if (cpu == null) { throw new argumentnullexception("cpu"); } if (gpu == null) { throw new argumentnullexception("gpu"); } if (hdd == null) { throw new argumentnullexception("hdd"); } if (ram == null) { throw new argumentnullexception("ram"); } if (power == null) { throw new argumentnullexception("power"); } if (case == null) { throw new argumentnullexception("case"); } if (motherboard.cpu.toupper().trim() != cpu.socket.toupper().trim()) { yield return new validationresult("cpu socket not match.", new[] { "cpu_id" }); } }
when create pc configuration without validation, goes database. when try validation says cpu (in null test) null , throws null exception. screenshot vs.
it never throws on motherboard on next item (on second item).
cpu seems treated entity framework navigation property. without more information suggests you'll need to:
using system.data.entity
in whatever unit selects model.include(c => c.cpu)
before enumerating query database
if that's navigation property, appears, may lazily loaded documented in entity framework materials.
this means though values seem exist in database actual collection --or in case, reference-- null in memory. tell entity framework "include" items when fetch:
var configuration = db.configurationsorsomething .include(c => c.cpu) .where(c => c.deletedorwhatever == false) .tolist(); var cpu = configuration.cpu; if(cpu == null) debug.writeline("something else problem. :(");
you did you've seen value in database. can't see code you've used select entity database --or else, if there else. can see cpu type you've defined , referenced. model looks ef code first / table-per-type if need know more along tutortial.
note: i've re-read comments in new light. when asked if getting database agreed "created normally" without validation.
i'm considering entire setup simple validation failure. if validation says cpu null... cpu null.
so: code using create new instance of configuration
entity? not assigning value cpu?
Comments
Post a Comment