EF包括孩子的几个导航属性集合

时间:2017-12-20 13:42:31

标签: c# entity-framework navigation-properties

我在Entity Framework中有一些奇怪的错误。我有下一个型号:

public class ModelLetterIncoming : ModelBase
{
    public ModelLetterIncoming()
    {
        attachedFiles = new List<ModelLetterIncomingAttachedFile>();
        contactPersons = new List<ModelContactPerson>();
    }

    public override int Id { get; set; }

    public DateTime DateAdd { get; set; }

    public string LetterText { get; set; }

    public bool IsNew { get; set; }

    public virtual ICollection<ModelContactPerson> contactPersons { get; set; }

    public virtual ICollection<ModelLetterIncomingAttachedFile> attachedFiles { get; set; }
}

public class ModelContactPerson : ModelBase
{
    public ModelContactPerson()
    {
        this.companies = new List<ModelCompany>();
        this.phones = new List<ModelContactPersonPhones>();
        this.emails = new List<ModelContactPersonEmails>();
    }

    public override int Id { get; set; }

    public string Name { get; set; }

    public string Login { get; set; }

    public virtual ICollection<ModelCompany> companies { get; set; }

    public virtual ICollection<ModelContactPersonPhones> phones { get; set; }

    public virtual ICollection<ModelContactPersonEmails> emails { get; set; }

    public int positionId { get; set; }

    public virtual ModelContactPersonsPositions position { get; set; }

    public override string ToString()
    {
        return this.Name;
    }
}

public class ModelContactPersonPhones : ModelBase
{
    public int contactPersonId { get; set; }

    public string phone { get; set; }

    public virtual ModelContactPerson contactPerson { get; set; }

    public override string ToString()
    {
        return this.phone;
    }
}

public class ModelContactPersonEmails : ModelBase
{
    public int contactPersonId { get; set; }

    public string email { get; set; }

    public virtual ModelContactPerson contactPerson { get; set; }

    public override string ToString()
    {
        return this.email;
    }
}

public class ModelCompany : ModelBase
{
    public ModelCompany()
    {
        this.contactPersons = new List<ModelContactPerson>();
    }

    public override int Id { get; set; }

    public string Name { get; set; }

    public string EDRPOU { get; set; }

    public string Postcode { get; set; }

    public int? cityId { get; set; }

    public virtual ModelCity city { get; set; }

    public int typeOfOwnershipId { get; set; }

    public virtual ModelTypeOfOwnership typeOfOwnership { get; set; }

    public string StreetName { get; set; }

    public string HouseNumber { get; set; }

    public string ApartmentNumber { get; set; }

    public string Address
    {
        get
        {
            return this.city + " " + this.StreetName + " " + this.HouseNumber + " " + this.ApartmentNumber;
        }
    }

    public int subscriberStatusId { get; set; }

    public virtual ModelSubscriberStatus subscriberStatus { get; set; }

    public string SubscriberCode { get; set; }

    public virtual ICollection<ModelContactPerson> contactPersons { get; set; }

    public override string ToString()
    {
        return this.Name;
    }
}

我在DbContext类中禁用了Lazy Loading,因为我为每个查询创建了一个新的DbContext实例:

public MysqlDbContext(string conenctionString) : base(conenctionString)
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Database.Connection.ConnectionString = conenctionString;
}

我使用这种方法来获取我的信件:

public static IQueryable<ModelLetterIncoming> GetLettersIncoming(IQueryable<ModelLetterIncoming> query)
    {
        IQueryable<ModelLetterIncoming> result = from letterIncoming in query.
            Include(letterIncoming => letterIncoming.attachedFiles).
            Include(letterIncoming => letterIncoming.contactPersons).
            Include(letterIncoming => letterIncoming.contactPersons.Select(contactPerson => contactPerson.phones)).
            //Include(letterIncoming => letterIncoming.contactPersons.Select(contactPerson => contactPerson.emails)).
            Include(letterIncoming => letterIncoming.contactPersons.Select(contactPerson => contactPerson.position))
            //Include(letterIncoming => letterIncoming.contactPersons.Select(contactPerson => contactPerson.companies.Select(company => company.city).Select(city => city.district).Select(district => district.region)))
            select letterIncoming;

        try
        {
            var tmp = result.ToList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        return result;
    }

问题是当我取消注释这一行时,

//Include(letterIncoming => letterIncoming.contactPersons.Select(contactPerson => contactPerson.emails)).

我无法调用ToList()并在IQueryable上获得结果。我有NullReferenceException: enter image description here

看起来我出于某种原因不能包含多个集合。

0 个答案:

没有答案