简体   繁体   English

EntityFramework未加载相关数据

[英]EntityFramework Not Loading Related Data

I have these entities: 我有这些实体:

public class Company : PrimaryKey
{
    public string Name {get;set;}

    public virtual Account Account {get;set;}
}

public class Account
{
    [Key]
    public Guid CompanyId { get; set; }

    public virtual Company Company {get;set;}
}

I use these configurations: 我使用以下配置:

modelBuilder.Entity<Company>()
    .HasOptional(c => c.Account)
    .WithRequired(a => a.Company)
    .WillCascadeOnDelete();

Now, I have two projects, one is a test bench project which is a Console Application with a DbContext and a Repository , the second is the full blown production project which is a MVC 4 in which I use Dependancy Injection to create a Repository .InTransientScope() which in turn loads a new context each time it is called. 现在,我有两个项目,一个是测试平台项目,它是带有DbContextRepositoryConsole Application ,第二个是完整的生产项目,它是MVC 4 ,其中我使用Dependancy Injection来创建存储库.InTransientScope()会在每次调用时加载一个新的上下文。

Both have exactly the same contexts and repositories (the product obviously has Interfaces). 两者具有完全相同的上下文和存储库(该产品显然具有接口)。

in the test bench when I call this: 在测试台上,当我这样称呼时:

_repository.GetById<Company>(id);

All of it properties are filled out, ie eager loading 它的所有属性均已填写,即渴望加载

in the production when I call the same line, nothing is loaded and its not loaded till I created another function which does this: 在生产中,当我调用同一行时,在我创建另一个执行此操作的函数之前,不会加载任何内容,也不会加载任何内容:

_dbContext.Companies.Include("Account").FirstOrDefault(x => x.Id.Equals(id));

Of which, when executed does provide all the Account information, but funnily bar any other navigation properties that Account contains!!! 其中,执行时确实提供了所有Account信息,但是很有趣地禁止了Account包含的任何其他导航属性! Even though I have disable LazyLoading, it still doesn't work. 即使我禁用了LazyLoading,它仍然无法正常工作。

This is surprising because both projects are fundamentally the same, bar the use of the IoC DI in one of them.... 这是令人惊讶的,因为两个项目在根本上是相同的,而其中一个却禁止使用IoC DI。

Why is this happening? 为什么会这样呢? How can I specify in a predominantly generic Repository to eager load all this information at the Controllers preference....? 如何在主要的通用存储库中指定要急于按Controllers首选项加载所有这些信息?

Break Points 断点

I set break points in both projects too look at the ADO.NET call to the database and the sql statement that was executed, in the test bench it did go off and call the information, in the production it did not show any joins or anything of that nature what so ever. 我在两个项目中都设置了断点,也查看了对数据库的ADO.NET调用和已执行的sql语句,在测试台中它确实关闭并调用了信息,在生产中它没有显示任何联接或任何内容。这样的性质。

Other Things Tried 其他尝试过的事情

I tried accessing the navigation property directly when loading it from the database: 从数据库加载导航属性时,我尝试直接访问它:

var acc = _repository.GetById<Company>(id).Account;

It still says null . 它仍然说null So my repository/context is not even loading any related data when asked for it.... what is going on?! 因此,当我要求我的存储库/上下文时,它甚至都没有加载任何相关数据。

Definitions 定义

_repository.GetById<Company>(id);

is: 是:

public T GetById<T>(Guid id)
{
    return _dbContext.Set<T>().FirstOrDefault(x => x.Id.Equals(id));
}

It's working now, I have no idea why.. I haven't tampered with anything. 它现在正在运行,我不知道为什么..我什么都没篡改。 The only thing that I have, was to put .InTransientScope() onto IDbContextFactory<MyContext> 我唯一拥有的就是将.InTransientScope() IDbContextFactory<MyContext>

I actually enabled all Lazy Loading everywhere I could, and it now works.... but it's strange that when I started on the Production project I never even tampered with Lazy Loading at all, but since I extented the Model and added modelBuilder stuff I have had to specifically tell it to Lazy Load. 我实际上在所有可能的地方都启用了所有延迟加载,现在它可以工作...。但是奇怪的是,当我开始生产项目时,我什至从未篡改过延迟加载,但是由于我扩展了Model并添加了modelBuilder东西,必须专门将其告知延迟加载。

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

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