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

查看:229
本文介绍了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?

下面是我的数据库设计(这也是显示在我的previously回答问题)

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

在我的code,我只是得到这样的会员单位:

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才能拥有的 FULL 实体?如果什么有另一个相关表语言表让说的功能的表。我应该这样做:

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长
我是pretty肯定(至少我真的希望)我是错的东西,但如果我们不能使用对象和它的子对象的选择,什么是具有EF的目的是什么?我们只能用子对象现场的使用(VAR上下文=新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;

...因为的延迟加载的试图从数据库加载语言导航属性,但与数据库的连接已经释放。您只能中使用这样的懒加载的你的使用块时,数据库的连接仍然是可用的。

...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.

您可以简化使用加载在你的榜样的导航属性的预先加载的有包含

You can simplify loading the navigation properties in your example by using eager loading with 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天全站免登陆