简体   繁体   English

.NET Entity Framework 代码优先:未加载导航属性

[英].NET Entity Framework code-first: navigation properties are not loaded

In my application I have many entities.在我的应用程序中,我有许多实体。

Two of them are Person and Company, which have a many-to-many relationship.其中两个是Person 和Company,它们是多对多的关系。

So in my code I have a Person class which contains a refeference to a collection of Companies (ICollection), and a Company class which contains a reference to a collection of people.因此,在我的代码中,我有一个 Person 类,其中包含对公司集合 (ICollection) 的引用,以及一个包含对人员集合的引用的 Company 类。 (ICollection). (ICollection)。

Using a first-code approach, the database was correctly generated.使用第一个代码方法,正确生成了数据库。 There are three tables: People, Companies and PeopleCompanies.共有三个表:人员、公司和人员公司。

In the table PeopleCompanies I have a record which should create an association between determined Person and an other determined Company, as shown in the image below.在 PeopleCompanies 表中,我有一条记录,它应该在确定的人员和其他确定的公司之间创建关联,如下图所示。

http://s27.postimg.org/xnkz3j94j/DBPrint.png http://s27.postimg.org/xnkz3j94j/DBPrint.png

So, what is my problem?那么,我的问题是什么?

In my business logic I need to know which people are associated to a determined Company, but the navigation property of the class Company remains empty: ICollection.Count() -----> 0在我的业务逻辑中,我需要知道哪些人与确定的公司相关联,但公司类的导航属性仍然​​为空:ICollection.Count() -----> 0

In realty, as you can see in the image above, there is exactly one person associated to the company.实际上,正如您在上图中所看到的,只有一个人与该公司相关联。

I need the navigation property!我需要导航属性!

Someone can help?有人可以帮忙吗?

The code of the DbContext is below. DbContext 的代码如下。 Thanks!谢谢!

 public class EnterpriseDataContext : DbContext
{
    public EnterpriseDataContext(string connString) : base(connString)
    {

    }


    #region CRM Module

    public DbSet<Category> Categories { get; set; }
    public DbSet<City> Cities { get; set; }
    public DbSet<Company> Companies { get; set; }
    public DbSet<CompanyNote> CompanyNotes { get; set; }
    public DbSet<Country> Countries { get; set; }
    public DbSet<Lead> Leads { get; set; }
    public DbSet<Person> People { get; set; }
    public DbSet<PersonNote> PersonNotes { get; set; }
    public DbSet<Sector> Sectors { get; set; }

    #endregion



}

My psychic debugging powers are telling me that lazy loading is enabled and you're not using the proper Includes.我的通灵调试能力告诉我延迟加载已启用并且您没有使用正确的包含。

First, you need to make sure you have the following using statement首先,您需要确保您有以下 using 语句

using System.Data.Entity

And then use .Include() to load your people.然后使用.Include()加载您的人员。

var myEnterpriseDataContext = new EnterpriseDataContext("insert connection string here");

myEnterpriseDataContext.Companies
    .Include(c => c.People)
    //.Rest Of Query

note: you don't need includes for fields that you only access while executing your query.注意:对于仅在执行查询时访问的字段,您不需要包含。 IE IE

myEnterpriseDataContext.Companies
    .Select(c => new CompanyViewModel(){People = c.People});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM