ASP.NET C#EF - 如何在选择完整对象后受益? [英] ASP.NET C# EF - How to benefit the full object after selecting it?

查看:132
本文介绍了ASP.NET C#EF - 如何在选择完整对象后受益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用实体框架处理ASP.NET C#,有些人非常关心某些关键点。

I've been working on ASP.NET C# with Entity Framework and some people help me very much on certain key points.

还有一点我不能充分了解当我们从EF中检索数据时,我们不应该使用对象及其子对象(外键关系表数据)?

There also one point I could not understand fully. Aren't we supposed to use the object and its subobjects (foreign key relation table data) when we retrieve the data from EF?

这是我的数据库设计(这是还显示在我以前回答的问题)

Here is my database design (which was also displayed in my previously answered question)

在我的代码中,我简单地得到这样的成员实体:

In my code, I simply get Member entity like this:

//In some aspx.cs file
var Mem = new MemberManager().GetById(2);

//In MemberManager class
public Member GetById(long id)
        {
            using(var Context = new NoxonEntities())
            {
                return Context.Member.First(c => c.Id == id);
            }
        }

当我这样做:

var Mem = new MemberManager().GetById(2);
var Lang = Mem.Language;
//I get 
//Error: 'Mem.Language' threw an exception of type 'System.ObjectDisposedException'

为了摆脱这个异常,我需要这样做:

In order to get rid of this exception I needed to do this:

public Member GetById(long id)
        {
            using(var Context = new NoxonEntities())
            {
                var Result = Context.Member.First(c => c.Id == id);
                //Getting the Language Entity
                Result.Language = Context.Language.First(c => c.Id == Result.LanguageId);

                return Result;
            }
        }

我必须运行一个SELECT才能拥有实体完整如果另一个相关表对于语言表,如果表示功能表,该怎么办?我应该这样做:

Do I have to run a SELECT in order to have FULL entity? What if there were ANOTHER related table to Language table lets say Feature Table. Should I do this:

public Member GetById(long id)
            {
                using(var Context = new NoxonEntities())
                {
                    var Result = Context.Member.First(c => c.Id == id);
                    //Getting the Language Entity
                    Result.Language = Context.Language.First(c => c.Id == Result.LanguageId);
                    Result.Language.Feature = Context.Language.Feature.First(c => c.Id == Result.FeatureId);
                    return Result;
                }
            }

哪些可以去verrry long和
我我非常确定(至少我真的希望)我错了,但是如果我们不能使用对象及其子对象选择后,EF的目的是什么?我们只能在网站上使用中的子对象(var Context = new NoxonEntities())块?

Which could go verrry long and I'm pretty sure (at least I genuinely hope) I am wrong about something, but if we cannot use the object and its subobjects AFTER selecting, what is the purpose of having EF? Can we only use the subobjects in site the using(var Context = new NoxonEntities()) block?

谢谢你,

推荐答案

将上下文视为数据库连接。当您使用语句在中创建上下文时,您可以使用此连接,并将其放在的末尾使用。使用块的之后的所有内容都不能再访问数据库连接。你在这里得到例外...

Think of the context as a database connection. You make this connection available to you when you create the context in the using statement and throw it away at the end of the using. Everything after the using block cannot access the database connection anymore. You get an exception here...

var Lang = Mem.Language;

...因为懒惰加载试图加载语言从数据库导航属性,但是已经处理与数据库的连接。在数据库连接仍然可用时,您可以使用 使用 c 块中的这种延迟加载。

...because lazy loading tries to load the Language navigation property from the database, but the connection to the database is already disposed. You can use such a lazy loading only within your using block when the connection to the database is still available.

您可以通过使用加载 Include

public Member GetById(long id)
{
    using(var Context = new NoxonEntities())
    {
        return Context.Member.Include("Language").First(c => c.Id == id);
    }
}

或者对于两个导航属性,使用:

Or for both navigation properties use:

return Context.Member.Include("Language.Feature").First(c => c.Id == id);

它会将成员加载到一个数据库中的语言和功能查询。

It will load the member and the language and feature in one single database query.

这篇关于ASP.NET C#EF - 如何在选择完整对象后受益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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