c# - Null list after query -
i have problem create object containing list, load database, run query returns object, find list null. other properties of object should be. i'm using list called "ints" filled few integers i've tried using other types.
here's model:
public class coursemodel { public int coursemodelid { get; set; } public virtual icollection<int> ints { get; set; } // variable in question public string name { get; set; } public string overview { get; set; } }
and here's database population (the database called learnyou):
public class learnyoudbcontextinitializer : dropcreatedatabasealways<learnyoudbcontext> { protected override void seed(learnyoudbcontext context) { context.courses.add(new coursemodel() { name = "c# programming", overview = "you'll learn c#", ints = new list<int> { 1, 42, 3 }, }); context.savechanges(); } }
here's controller code querying object:
// get: course/edit/5 public actionresult edit(int? id) { if (id == null) { return new httpstatuscoderesult(httpstatuscode.badrequest); } coursemodel coursemodel = db.courses.find(id); // debugging previous line shows ints null if (coursemodel == null) { return httpnotfound(); } return view(coursemodel); }
the "ints" property not null after saving context in database population part null when it's queried (i visit page ~edit/1 debug). can't figure out why when other properties fine. ideas? thanks.
an icollection
in model indicates parent->child relationship. however, doubt ef able determine how create child table icollection
of integers. here do.
create new model ints (or whatever represents):
public class ints { public int value { get; set;} }
modify original model use it:
public class coursemodel { public int coursemodelid { get; set; } public virtual icollection<ints> ints { get; set; } // see difference? public string name { get; set; } public string overview { get; set; } }
that should make work.
Comments
Post a Comment