实体框架代码返回空字段的第一个导航属性 [英] Entity Framework Code First navigational properties returning null fields

查看:134
本文介绍了实体框架代码返回空字段的第一个导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于程序,我第一次使用EF代码。在过去我使用linq到SQL en EF DbFirst。当检索主记录时,我无法使用导航属性加载子记录。我得到一个空的子记录,所有记录字段为0或为空。

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; }

与以下上下文:

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

使用延迟加载我的所有属性的交付类都是null使用以下代码:

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();
        }

在我的调试器的行上告诉我交付存在,所有的属性都为null。

on the rows with the ** my debugger is telling me that delivery exists but all of it's properties are null.

如果要应用include,则.include不显示导航:

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??

推荐答案

您必须从记录中删除导航引用的初始化构造函数,空导航集合的初始化可以:

You must remove the initialization of the navigation references from the Record constructor, the initialization of the empty navigation collection is OK:

Shipping = new ShippingData(); // remove this line
Delivery = new DeliveryData(); // remove this line

本质上这些行将加载数据与 code> ShippingData 和 DeliveryData 设置,可能是默认值 0 null

Essentially these lines "overwrite" the loaded data with values that the constructors of ShippingData and DeliveryData set, probably the default values 0 and null.

关于你的评论的旁注.include(x => x .... ...)没有显示我的导航。您需要使用System.Data.Entity; 在代码文件的开头放置,使 Include 扩展方法可以使用lambda表达式作为参数。

Side note about your remark "the .Include(x=>x.......) isn't showing my navigationals". You need to put using System.Data.Entity; at the beginning of your code file to make the Include extension method available that takes a lambda expression as parameter.

这篇关于实体框架代码返回空字段的第一个导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆