简体   繁体   English

C#实体框架延迟加载未加载

[英]C# Entity Framework Lazy Loading Isn't Loading

I've done all the readings I can find on Entity and lazy loading, as well as other questions here, but for the life of me I can't get this working. 我已经完成了我在Entity和延迟加载上找到的所有读数,以及其他问题,但是对于我的生活,我无法实现这一点。 Here's my SQL for the DB: 这是我的DB的SQL:

CREATE TABLE corporations(
corporationID bigint PRIMARY KEY
);

CREATE TABLE Character(
personID bigint PRIMARY KEY,
corporationID int NOT NULL,
FOREIGN KEY (corporationID) REFERENCES corporations(corporationID)
);

And the Entity code to get it ( * EDITED from original, still broken * ): 和获取它的实体代码( * 从原始编辑,仍然损坏 * ):

DBEntities context = new DBEntities();
public Character Character_GetByID(long CharacterID)
        {
            context.ContextOptions.LazyLoadingEnabled = true;
            Character character = context.Characters.Where(c => c.CharacterID == CharacterID).FirstOrDefault();      
            return character;
        }

So from my understanding, with this I should be able to go 所以从我的理解,我应该能够去

Character char = Character_GetByID(characterID);
Corporation corp = char.Corporation;

The "char.Corporation" object exists, Entity created it properly from the foreign key. 存在“char.Corporation”对象,Entity从外键正确创建它。 But when I run the above code, "corp" always returns as NULL (even though I know for sure that the relevant corporation is in the DB). 但是当我运行上面的代码时,“corp”总是返回NULL(即使我确定相关公司在DB中)。

One thing I did notice is that in the auto-generated Entity Character object, it has the function: 我注意到的一件事是在自动生成的Entity Character对象中,它具有以下功能:

public virtual Corporation Corporation
        {
            get { return _corporation; }
            set
            {
                if (!ReferenceEquals(_corporation, value))
                {
                    var previousValue = _corporation;
                    _corporation = value;
                    FixupCorporation(previousValue);
                }
            }
        }

Which seems odd, because I would assume that with lazy loading the "get" function would be something like "if null, try to get Corporation from database". 这看起来很奇怪,因为我认为在延迟加载时,“get”函数会像“if null,尝试从数据库中获取公司”。 Any thoughts would be highly appreciated. 任何想法都将受到高度赞赏。

* EDIT * Request for how lazy loading is configured: * 编辑 *请求如何配置延迟加载:

In my Context class, for every constructor I have 在我的Context类中,对于我拥有的每个构造函数

this.ContextOptions.LazyLoadingEnabled = true;

And as you can see from the first C# function up above, I tried setting it as true in the function itself, just before querying it. 正如您从上面的第一个C#函数中可以看到的那样,我在函数本身中尝试将其设置为true,就在查询之前。

Lazy loading is a functional provided by object context (DBEntities class instance in your case). 延迟加载是对象上下文(在您的情况下为DBEntities类实例)提供的功能。 So, it will work only when entity object is attached to some DBEntities instance. 因此,仅当实体对象附加到某个DBEntities实例时才会起作用。 When Character_GetByID method is done, context is disposed, entity is detached, and your lazy loading request cannot be performed. 完成Character_GetByID方法时,将释放上下文,分离实体,并且无法执行延迟加载请求。

Remove the using statement, as when you use this dbcontext is disposed right away 删除using语句,就像使用此dbcontext时一样

using (var context = new DBEntities())
{
   ...
}//context is disposed here... lazy loading is not possible

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

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