【问题标题】:objectcontext disposed exceptionobjectcontext 处理异常
【发布时间】:2013-05-24 03:15:39
【问题描述】:

每个用户都有一个功能(学生、开发人员、演员...),所以 Users 中的每个用户都有 int 属性,它是 Functions 中的外键,每个功能在每种文化中都有多个名称,所以 Functions 有 int 属性这是 Functions_Translations 中的外键,这里我有每个 Culture 中的函数名称

在我的控制器中:

        Users user = new Users();
        using (var db = new MyAppContext())
        {
            user = db.Users.FirstOrDefault(u => u.Mail == "tester@test.test");
        }
        return View(user );

在我看来:

@model Models.Users
...
@Model.Functions.Functions_Translations.Select( x => x.FunctionId == Model.FunctionId  & x.CultureId == 1).First()... (don't know how do show the attribute Name in Functions_Translations )

我有两个问题: 如何在 Functions_Translation 中显示 Name 属性以及第二个也是最重要的:

在视图中我收到以下错误:ObjectContext 实例已被释放,不能再用于需要连接的操作。

我看到了很多类似的解决方案:

make user var and do return view(user.tolist())

但在我看来,这迫使我做 foreach。还有其他解决方案吗?

对不起,我的英语不好,谢谢

【问题讨论】:

    标签: .net c#-4.0 asp.net-mvc-4


    【解决方案1】:

    请设置以下并尝试一次。

    db.ContextOptions.LazyLoadingEnabled= false;

    将此行用作using 块内的第一行,然后执行其他操作。对于第一个问题,请通过提供Functions_Translations 模型的属性来澄清,并解释您要完成的工作。

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多