简体   繁体   English

Linq查询引用不同的上下文,但实际上不是

[英]Linq queries references different contexts but actually it is not

I am trying to get data from two different tables using EF 6, it is a asp.net mvc 5 project with Identity 2.0 used in it, however when i join two tables, i am getting the error that contexts of both entities are different, however it is not, here is my code: 我正在尝试使用EF 6从两个不同的表中获取数据,这是一个使用了Identity 2.0的asp.net mvc 5项目,但是当我加入两个表时,出现了两个实体的上下文不同的错误,但是不是,这是我的代码:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public DbSet<UserAccountStatus> UserAccountStatuss { get; set;}

    public static ApplicationDbContext Create()
    {
       return new ApplicationDbContext();
    }
}

and here is my linq query : 这是我的linq查询:

var result = (from user in DbContext.Users
              join accountStatus in DbContext.UserAccountStatuss on user.Id equals accountStatus.UserId
              where user.Email == email
              select accountStatus.AccountEnabled).FirstOrDefault();

My DbContext property: 我的DbContext属性:

public ApplicationDbContext DbContext
{
    get
    {
        return _dbContext ?? ApplicationDbContext.Create();
    }
    private set
    {
        _dbContext = value;
    }

}

Exact error is : 确切的错误是:

The specified LINQ expression contains references to queries that are associated with different contexts. 指定的LINQ表达式包含对与不同上下文关联的查询的引用。

and this is not a duplicate, i have seen other questions, in those user are actually trying different contexts, but in my case i have one. 这不是重复的,我看过其他问题,在那些用户实际上尝试不同的上下文的情况下,但就我而言,我有一个问题。

your lazy instantiaiton is the problem. 你的懒惰实例化就是问题所在。 It should be 它应该是

private ApplicationDbContext _dbContext;
public ApplicationDbContext DbContext
{
    get
    {
        if(_dbContext==null){_dbContext=ApplicationDbContext.Create();}
        return _dbContext;
    }


}

Edit: Example with Lazy 编辑:懒惰的例子

private Lazy<ApplicationDbContext> _dbContext=new Lazy<ApplicationDbContext>(()=>ApplicationDbContext.Create());
public ApplicationDbContext DbContext
{
    get
    {
        return _dbContext.Value;
    }


}
public ApplicationDbContext DbContext
{
    get
    {
        return _dbContext ?? ApplicationDbContext.Create();
    }
    private set
    {
        _dbContext = value;
    }

}

You get a new context every time... 每次您都会获得新的环境...

public ApplicationDbContext DbContext
{
    get
    {
        if(_dbContext == null)
            _dbContext = ApplicationDbContext.Create();
        return _dbContext;
    }    
}

Try this 尝试这个

Your property will return a new context each time it is accessed. 您的媒体资源每次访问都会返回一个新的上下文。 You need to store the newly created context so it can be returned the next time it is used. 您需要存储新创建的上下文,以便下次使用它时可以将其返回。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ表达式包含对与不同上下文关联的查询的引用 - The LINQ expression contains references to queries that are associated with different contexts 错误:“指定的LINQ表达式包含对与不同上下文关联的查询的引用” - Error: “The specified LINQ expression contains references to queries that are associated with different contexts” LINQ 表达式包含对与不同上下文关联的查询的引用 - LINQ expression that contains references to queries that are associated with different contexts 为什么我会收到指定的 LINQ 表达式包含对与不同上下文关联的查询的引用? - Why do I receive The specified LINQ expression contains references to queries that are associated with different contexts? 将两个模型合并在一起会导致“指定的LINQ表达式包含对与不同上下文关联的查询的引用。” - Union two models together causes “The specified LINQ expression contains references to queries that are associated with different contexts.” 错误-“指定的LINQ表达式包含对与不同上下文关联的查询的引用。” - Error-“The specified LINQ expression contains references to queries that are associated with different contexts.” 错误:指定的 linq 表达式包含对与不同上下文关联的查询的引用 - Error: the specified linq expression contains references to queries that are associated with different context LINQ 使用 2 个不同的数据库上下文排除记录 - LINQ Exclude records using 2 different database contexts 第一次调用后,对不同数据库上下文的 EF 查询似乎会缓存 - EF queries to different DB contexts seem to cache after the first call LINQ使用接口针对两个不同的数据上下文 - LINQ against two different data contexts using interfaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM