实体框架6上下文中检索导航属性 [英] Entity Framework 6 context not retrieving navigation properties

查看:189
本文介绍了实体框架6上下文中检索导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有很多的其他职位,但他们面临新台币完全一样的问题。并且它们使用稍微不同的代码。所以我认为这是值得检讨这一点。

I have found many other posts but they are nt facing exactly the same problem. And they are using a slightly different code. SO I think it is worth reviewing this.

I'm使用EF6代码首先,我创建了一个具有一定的导航性能的客户实体。

I´m using EF6 code first, and I created a Client Entity that has some navigation properties.

I'll刚刚发布的相关代码,可考虑有一些更多的属性键和外键为好,但问题并不相关。模型生成ok了。

I´ll post just the relevant code, consider there are a few more properties and foreign keys as well, but not relevant to the problem. Model is generating ok.

public class Client
{
    public Client()
    {
        JobsExperiences = new Collection<JobsExperience>();
        CapacitationCourses = new Collection<CapacitationCourse>();
        ScholarLevelDetails = new Collection<ScholarLevelDetail>();
        Relatives = new Collection<Relative>();
    }
    public long ClientID { get; set; }
    public virtual ICollection<ScholarLevelDetail> ScholarLevelDetails { get; set; }

    public virtual ICollection<JobsExperience> JobsExperiences { get; set; }
}

现在我创建了一个ClientServices类,我把那得到或发送的所有方法往返于数据库的数据,疗法我有这样的代码,这是工作随机,I'll尽量解释清楚。

Now I created a ClientServices class where I put all methods that get or send data from and to the data base., ther I have this code, which is working randomly, I´ll try to explain clearly.

    internal Client GetClient(string userId, bool lazyLoadingEnabled = true)
    {
        using (var context = new ApplicationDbContext())
        {
            context.Configuration.LazyLoadingEnabled=lazyLoadingEnabled;

            var client = (from _client in context.Client
                          where _client.ApplicationUserId == userId
                          select _client).FirstOrDefault();

            return client;
        }
    }



我的目标有些情况下是只获得客户属性,有时所有的属性,包括导航属性。

My objective some cases is to retrieve just the client attributes, and sometimes all attributes including navigation properties.

在我的控制器我有一个这样的行

In my controller I have a line like this

var client = uuc.GetClient(user.Id, false);

或本

var client = uuc.GetClient(user.Id);

当我运行的第一句话,导航属性初始化但都有数= 0,即使我的数据库有相关的记录。我想,如果延迟加载被禁用,这意味着预先加载已启用,但似乎并非如此。但是,在导航性能给力的负载没有load()方法。

When I run the first sentence, the navigation properties are initialized but all has Count=0, even when my DB has records associated. I think, if lazy loading is disabled, it means eager loading is enabled, but it seems not. However, there is no Load() Method in the navigation properties to force load.

当我运行第二句,导航性能会抛出异常的客户端.ScholarLevelDetails'扔类型的异常System.ObjectDisposedException。这是抛出了一句后一条线,在手表的导航属性寻找热塑成型。然而,这是怪异的一部分,如果我一步回了句和调试步入方法,所有的导航属性都正确。

When I run the second sentence, the navigation properties throws an exception 'client.ScholarLevelDetails' threw an exception of type 'System.ObjectDisposedException'. This is thrown one line after the sentence, loking at the navigation properties in the watch. However, and this is the weirdest part, if I step back to the sentence and debug stepping into the method, All navigation properties are loaded correctly.

为什么代码具有不同如果不是运行步入方法运行一次?
我相信之前的导航性能负载使用报表范围完成,但为什么禁用加载奠定能源部鼻涕要么找回它们?
我如何编写这有一个一致的行为?

Why the code behaves differently if running at once than running stepping into the method? I presume the using statement scope finishes before that the navigation properties load, but why disabling lay loading doe snot retrieve them either? How can I code this to have a consistent behaviour?

推荐答案

我更改查询代码Ihad Linq中与此之一。

I change the query code Ihad in Linq with this one.

    internal Client GetClient(string userId, bool lazyLoadingEnabled = true)
    {
        using (var context = new ApplicationDbContext())
        {
            context.Configuration.LazyLoadingEnabled = lazyLoadingEnabled;

            var client = context
                        .Client
                        .Include(s => s.ScholarLevelDetails)
                        .Include(s => s.JobsExperiences)
                        .Include(s => s.CapacitationCourses)
                        .Include(s => s.Relatives)
                        .FirstOrDefault(s => s.ApplicationUserId == userId);

            return client;
        }
    }

和现在的作品。然而,我仍然有我倒是LVE与你的读者和同事讨论一些问题。

And now it works. however I still have some questions I´d lve to discuss with you readers and colleagues.

为什么普通的LINQ doesn't工作?
为什么如果启用了惰性加载或事项不没有关系,这段代码的工作原理相同,每次?

Why plain Linq doesn´t work? Why it doesn matter if lazyloading is enabled or not, this code works the same everytime?

这篇关于实体框架6上下文中检索导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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