Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework Code First navigational properties returning null fields
    primarykey
    data
    text
    <p>For a program I'm using EF code first for the first time. In the past I used linq to SQL en EF DbFirst. When retreiving the main record'I'm unable to load the subrecords using the navigational properties. I get an empty subrecord with all record-fields are 0 or null.</p> <p>When I want to apply eager loading. the .Include(x=>x.......) isn't showing my navigationals.</p> <p>I have set up the following classes:</p> <pre><code>public Record() { Shipping = new ShippingData(); Delivery = new DeliveryData(); Items = new List&lt;Item&gt;(); ImportDate = DateTime.Now; } [Key] public int RecordId { get; set; } public int ShippingId { get; set; } public int DeliveryId { get; set; } public DateTime ImportDate { get; set; } public virtual ShippingData Shipping { get; set; } public virtual DeliveryData Delivery { get; set; } public virtual List&lt;Item&gt; Items { get; set; } public virtual Collecting CollectingOrder { get; set; } </code></pre> <p>with the following context:</p> <pre><code>public class TweemansContext : DbContext { public TweemansContext() : base("xxxx") { } public DbSet&lt;Add.Record&gt; Records { get; set; } public DbSet&lt;Add.ShippingData&gt; Shipping { get; set; } public DbSet&lt;Add.DeliveryData&gt; Delivery { get; set; } public DbSet&lt;Add.Item&gt; ProductItems { get; set; } public DbSet&lt;Add.Kolli&gt; Kolli { get; set; } public DbSet&lt;Add.Collecting&gt; CollectingOrders { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity&lt;Record&gt;().HasRequired(s =&gt; s.Shipping) .WithMany() .HasForeignKey(s =&gt; s.ShippingId); modelBuilder.Entity&lt;Record&gt;().HasRequired(d =&gt; d.Delivery) .WithMany() .HasForeignKey(d =&gt; d.DeliveryId); modelBuilder.Entity&lt;Record&gt;().HasOptional(c =&gt; c.CollectingOrder) .WithOptionalDependent(a =&gt; a.Record).Map(p =&gt; p.MapKey("CollectionID")); } } </code></pre> <p>the delivery class is a public class as wel as follows:</p> <pre><code> public class DeliveryData { [Key] public int DeliveryId { get; set; } public virtual Record Record { get; set; } ....lots of public properties </code></pre> <p>now when I try to use lazy loading all my properties of the delivery class are null using the following code:</p> <pre><code>using (TweemansContext context = new TweemansContext()) { var imported = (from record in context.Records where record.ImportDate.Year == date.Year &amp;&amp; record.ImportDate.Month == date.Month &amp;&amp; record.ImportDate.Day == date.Day select record).ToList(); foreach (var Record in imported) { string email; string telnr; CustomerService service = new CustomerService(connectionString); **string servicenr = Record.Delivery.AccountNumber.Substring(2, 8);** service.GetUserData(servicenr, out email, out telnr); Record.Delivery.Email = email; Record.Delivery.TelephoneNbr = telnr; } context.SaveChanges(); } </code></pre> <p>on the rows with the ** my debugger is telling me that delivery exists but all of it's properties are null.</p> <p>When wanting to apply an include as follows the .include isn't showing my navigation:</p> <pre><code> var imported = (from record in context.Records.Include(x=&gt;x.) where record.ImportDate.Year == date.Year &amp;&amp; record.ImportDate.Month == date.Month &amp;&amp; record.ImportDate.Day == date.Day select record).ToList(); </code></pre> <p>What am I doing wrong, or which part have i misuderstood??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload