c# - code first get values from foreign key constraint -
i've started sample project code first , null value entity has foreign key constraint.
my model:
public class customeritem { [key] public int id { get; set; } public int name{get;set;} public virtual ienumerable<orderitem> order { get; set; } } public class orderitem { [key] public int id { get; set; } [foreignkey("customeritem")] public int customerid { get; set; } public virtual customeritem customeritem { get; set; } }
dataaccess linq query values:
public ienumerable<customeritem> getordersfromcustomeritem(int id) { return (from c in this.dax.customer c.id.compareto(id) == 0 select c).asenumerable().tolist(); }
this query return null orders think there's mistake in query. how can wire entity foreign key table?
thanks
updated query customers (and orders) this:
public ienumerable<customeritem> getallcustomers() { return (from c in this.dax.customer select new { //properties orderitem = c.orderitem }).asenumerable.tolist().select(s => new customeritem { //properties orderitem = s.orderitem, //here got exception wrote in comment }).tolist(); }
change:
public virtual ienumerable<orderitem> order { get; set; }
to:
public virtual icollection<orderitem> order { get; set; }
since ienumerable<>
exposes read-only methods, entity framework
ignoring it. property must of writeable collection type in order map database.
it quite common though, expose ienumerable
property, , have it's getter
returning backing-field
ilist/icollection
.
Comments
Post a Comment