Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从模型到控制器的数据_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 从模型到控制器的数据

C# 从模型到控制器的数据,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我的控制器中有这个功能,但我希望遵循最佳实践,并将数据库逻辑放入模型中 我想将所有数据库逻辑select、update、delete、insert放到模型中,因此我在模型中创建了方法 我检索数据的方法: public IQueryable<ChatLogsNameViewModel> getChatLogWithName() { using (var db = new ChatLogContext()) { var list = (from b in

我的控制器中有这个功能,但我希望遵循最佳实践,并将数据库逻辑放入模型中

我想将所有数据库逻辑select、update、delete、insert放到模型中,因此我在模型中创建了方法

我检索数据的方法:

public IQueryable<ChatLogsNameViewModel> getChatLogWithName()
{
    using (var db = new ChatLogContext())
    {
        var list = (from b in db.ChatLogs
                    select new ChatLogsNameViewModel()
                    {
                        UserProfile = b.UserProfile,
                        Message = b.Message,
                        Time = b.Time
                    });

        return list;
    }
}
我在控制器中调用getChatLogWithName方法,如下所示:

List<ChatLogsNameViewModel> items = null;
using (var dba = new ChatLogContext())
{
    items = dba.getChatLogWithName().ToList();
    return View(items);
}
var repo = new Repo();
var items = repo.getChatLogWithName().ToList();
我得到的错误是:

无法完成该操作,因为DbContext已被释放

正确的方法是什么?我只想通过连接将两个表中的所有记录传递给控制器

public IQueryable<ChatLogsNameViewModel> getChatLogWithName()
    {

            var list = (from b in this.ChatLogs
                        select new ChatLogsNameViewModel()
                        {
                            UserProfile = b.UserProfile,
                            Message = b.Message,
                            Time = b.Time
                        });

            return list;
    }
上下文的生存期从实例创建和运行时开始 当实例被释放或垃圾回收时结束。使用 如果希望上下文控制的所有资源都是 设置在块的末端。使用时,编译器 在中自动创建try/finally块并调用dispose 最后一个街区

问题是当内部using被释放时,它使DbContext无效。因此,您需要使用.ToList将查询结果保存在内存中

假设在名为Repo的类中定义了getChatLogWithName,则可以将控制器逻辑更改为如下内容:

List<ChatLogsNameViewModel> items = null;
using (var dba = new ChatLogContext())
{
    items = dba.getChatLogWithName().ToList();
    return View(items);
}
var repo = new Repo();
var items = repo.getChatLogWithName().ToList();
或移动。ToList至getChatLogWithName

顺便说一句,您不应该使用嵌套的DbContextscope,在您的控制器中,您不必使用另一个DbContextscope来包装它

上下文的生存期从实例创建和运行时开始 当实例被释放或垃圾回收时结束。使用 如果希望上下文控制的所有资源都是 设置在块的末端。使用时,编译器 在中自动创建try/finally块并调用dispose 最后一个街区

问题是当内部using被释放时,它使DbContext无效。因此,您需要使用.ToList将查询结果保存在内存中

假设在名为Repo的类中定义了getChatLogWithName,则可以将控制器逻辑更改为如下内容:

List<ChatLogsNameViewModel> items = null;
using (var dba = new ChatLogContext())
{
    items = dba.getChatLogWithName().ToList();
    return View(items);
}
var repo = new Repo();
var items = repo.getChatLogWithName().ToList();
或移动。ToList至getChatLogWithName


顺便说一句,您不应该使用嵌套的DbContextscope,在您的控制器中,您不必使用另一个DbContextscope来包装它。

以确保处理后不会引用DBContext。返回一个列表,这样你就不必打电话了。ToList:

既然dba和db看起来是一样的,您就不能将代码更改为使用dba实例,直到控制器中的using语句结束后,它才会被释放

public IQueryable<ChatLogsNameViewModel> getChatLogWithName()
    {

            var list = (from b in this.ChatLogs
                        select new ChatLogsNameViewModel()
                        {
                            UserProfile = b.UserProfile,
                            Message = b.Message,
                            Time = b.Time
                        });

            return list;
    }

以确保处置后不会引用DBContext。返回一个列表,这样你就不必打电话了。ToList:

既然dba和db看起来是一样的,您就不能将代码更改为使用dba实例,直到控制器中的using语句结束后,它才会被释放

public IQueryable<ChatLogsNameViewModel> getChatLogWithName()
    {

            var list = (from b in this.ChatLogs
                        select new ChatLogsNameViewModel()
                        {
                            UserProfile = b.UserProfile,
                            Message = b.Message,
                            Time = b.Time
                        });

            return list;
    }

控制器是一次性的。将上下文设置为一个字段,并在处理控制器时处理它。我是初学者,您能详细说明一下吗?谢谢你。@abatishchev 3标题中的内容…@sza:对不起,输入错误。谢谢,修好了!控制器是一次性的。将上下文设置为一个字段,并在处理控制器时处理它。我是初学者,您能详细说明一下吗?谢谢你。@abatishchev 3标题中的内容…@sza:对不起,输入错误。谢谢,修好了!是的,我犯了一个错误,不知道在这种情况下什么时候处理火灾。我明白你的意思。我可以使用全局上下文,但我认为为了更安全,必须在控制器中使用。是的,我犯了一个错误,不知道在这种情况下何时处理火灾。我明白你的意思。我可以使用全局上下文,但我认为为了更安全,您必须在控制器中使用。是的,我同意,我认为使用总是更安全的方式,所以您不需要在控制器中使用?将私有实例放在顶部就足够了?使用更安全。但是,它不允许访问WebAPI OData工作所需的IQueryable。是的,我同意,我认为使用总是更安全的方式,所以您不需要在控制器中使用?将私有实例放在顶部就足够了?使用更安全。但是,它不允许访问WebAPI OData工作所需的IQueryable。