在实体框架中不使用Dispose或Using() [英] Not Using Dispose or Using() in Entity Framework

查看:85
本文介绍了在实体框架中不使用Dispose或Using()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个Web应用程序并学习实体框架。如果我做错了事,我很好奇。我在查询时没有使用过dispose或语句。

I was writing a web app and learning entity framework along the way. I am curios if I have done something wrong though. I haven't been using dispose or using statements when querying.

我的存储库示例:

public User GetUserById(int sessionId)
{
    var user = (from u in db.Users where u.Id == sessionId select u).SingleOrDefault();
    return user;
}

我从中选择的用户表将具有与其他表关联的外键表。我认为关于实体的最酷的事情是,我可以返回整个User对象(由实体构建),然后让我做这样的事情 User.MyOtherTable.SomeColumn 在我的控制器中。

My User table I selected from would have foreign keys which would be associated with other tables. The cool thing, or so I thought, about entity is that I could return the entire User object (built by entity) which then would allow me to do something like this User.MyOtherTable.SomeColumn in my controller.

问题是我依赖于此,并在我的控制器中以一种很快乐的方式来获取用户信息以及利用来自其他表。我现在意识到,如果像下面的代码那样关闭该连接:

The problem is I relied on this and went about my merry way in my controller of grabbing the user information as well as utilizing information from the other table. I am now realizing that if I close that connection like the code below:

public User GetUserById(int sessionId)
{   using(db)
    {
    var user = (from u in db.Users where u.Id == sessionId select u).SingleOrDefault();
    return user;
    }
}

我的控制器不再有权访问 User.MyOtherTable.SomeColumn ,因为它为空。我真正的问题是,对我而言,在实体应用程序中使用处置有多重要?

My controller no long has access to User.MyOtherTable.SomeColumn as this will be null. My true question is how important is it for me use dispose in my entity application?

推荐答案

我强烈建议您使用语句,并且您不再依赖延迟加载。

I would strongly recommend that you use using statements, and also that you stop relying on lazy-loading.

而不是选择具有延迟加载的 User 对象属性,计算出所需的全部数据,并将其投影到Model类,例如

Instead of selecting User objects with lazy-load properties, work out the full data you need and project that to a Model class, e.g.

public class UserWithOtherStuffModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string OtherStuff { get; set; }
}

然后在您的方法中:

public UserWithOtherStuffModel GetUserById(int sessionId)
{
    using(db)
    {
        var user = (from u in db.Users 
            where u.Id == sessionId 
            select new UserWithOtherStuffModel{
                Id = u.Id,
                Name = u.Name,
                OtherStuff = u.MyOtherTable.SomeColumn
            }).SingleOrDefault();
        return user;
    }
}

这不仅意味着您限制了数据库的数量调用,但是您的数据合同与数据库模型无关。如果移动该列,只需更新查询以指向新的地点/名称,视图就可以保持不变。

Not only does this mean you limit the number of database calls, but you have a data contract that isn't tied to your database model. If you move that column, you simply update the query to point to the new spot/name, the view can remain unchanged.

这篇关于在实体框架中不使用Dispose或Using()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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