选择时出现EF错误:由于对象的当前状态,操作无效。没有SingleOrDefault,没有更新

时间:2015-12-10 08:11:23

标签: entity-framework linq-to-entities

由于以下情况,我有这样的错误: 我在数据库中有3个表。用户,应用程序,UserApplication。 MyDbConext有像DbSet这样的表。 UserApplication表具有FK到User表和FK到Application表。

public MyDbContext DbContext {get; set}
...........
public override IQueryable<User> Users
{
      get
      {
           var result = from user in DbContext.Users
                        where user.id == "some int variable"
                        select user;
                return result;
            }
        }
}
public override IQueryable<UserApplication> UserApplications
{
      get
      {
           var result = from app in DbContext.UserApplications
                        join user in Users on app .UserId equals user.Id
                        select app;
                return result;
      }
}
public override IQueryable<Application> Applications
{
      get
      {
           var result = from app in DbContext.Applications
                        join userApp in UserApplications 
                             on app.Id equals userApp.ApplicationId
                        select unsApp;
                return result;
       }
}

当我调用Applications.ToList()时,由于对象的当前状态异常,操作无效。如果我将应用程序重写为:

public override IQueryable<Application> Applications
{
      get
      {
           var result = from app in DbContext.Applications
                        join userApp in DbContext.UserApplications 
                             on app.Id equals userApp.ApplicationId
                        join user in Users on userApp.UserId equals user.Id
                        select unsApp;
                return result;
       }
}
一切正常。有人可以解释为什么会发生异常吗?

0 个答案:

没有答案