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.
When I want to apply eager loading. the .Include(x=>x.......) isn't showing my navigationals.
I have set up the following classes:
public Record()
{
Shipping = new ShippingData();
Delivery = new DeliveryData();
Items = new List<Item>();
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<Item> Items { get; set; }
public virtual Collecting CollectingOrder { get; set; }
with the following context:
public class TweemansContext : DbContext
{
public TweemansContext()
: base("xxxx")
{
}
public DbSet<Add.Record> Records { get; set; }
public DbSet<Add.ShippingData> Shipping { get; set; }
public DbSet<Add.DeliveryData> Delivery { get; set; }
public DbSet<Add.Item> ProductItems { get; set; }
public DbSet<Add.Kolli> Kolli { get; set; }
public DbSet<Add.Collecting> CollectingOrders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Record>().HasRequired(s => s.Shipping)
.WithMany()
.HasForeignKey(s => s.ShippingId);
modelBuilder.Entity<Record>().HasRequired(d => d.Delivery)
.WithMany()
.HasForeignKey(d => d.DeliveryId);
modelBuilder.Entity<Record>().HasOptional(c => c.CollectingOrder)
.WithOptionalDependent(a => a.Record).Map(p => p.MapKey("CollectionID"));
}
}
the delivery class is a public class as wel as follows:
public class DeliveryData
{
[Key]
public int DeliveryId { get; set; }
public virtual Record Record { get; set; }
....lots of public properties
now when I try to use lazy loading all my properties of the delivery class are null using the following code:
using (TweemansContext context = new TweemansContext())
{
var imported = (from record in context.Records
where record.ImportDate.Year == date.Year && record.ImportDate.Month == date.Month && 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();
}
on the rows with the ** my debugger is telling me that delivery exists but all of it's properties are null.
When wanting to apply an include as follows the .include isn't showing my navigation:
var imported = (from record in context.Records.Include(x=>x.)
where
record.ImportDate.Year == date.Year
&& record.ImportDate.Month == date.Month
&& record.ImportDate.Day == date.Day
select record).ToList();
What am I doing wrong, or which part have i misuderstood??