c# - EF4.1 - Attribute Evaluating to null at runtime -


i'm using ef4.1 code first create simple database app sql ce 4 backend. have product class , callitem class defined so:

    class callitem     {         public int id { get; set; }         public float discount { get; set; }          public virtual product product { get; set; }     }      class product     {         public int id { get; set; }          public decimal basecost { get; set; }         public int unitsize { get; set; }         public bool iswasteoil { get; set; }          public string code { get; set; }         public string name { get; set; }         public string description { get; set; }         public string ingredients { get; set; }     } 

edit - when creating collection of callitems using linq query, cannot access attributes of product attached each callitem, eg

var callitems = ci in context.callitems select ci;  foreach(callitem callitem in callitems) {     runsheet nrs = new runsheet();     nrs.prodcode = callitem.product.code; } 

interrogating database shows productid in callitems being populated. however, following line generates nullreferenceexception during run time:

nrs.prodcode = callitem.product.code; 

because callitem.product evaluating null. lazy loading , if how can resolve issue?

runsheet class, nrs instance attribute 'prodcode' want populate callitem's product's code.

thanks!

from code you've showed should work. have tried explicit loading?

var callitems = ci in context.callitems.include(c => c.product) select ci;  foreach(callitem callitem in callitems) {     runsheet nrs = new runsheet();     nrs.prodcode = callitem.product.code; } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -