使用ADO.NET实体模型时出错。 (实体类型vw_facilitydepartmentrights不是当前上下文的模型的一部分。) [英] Getting error while using ADO.NET entity model. (The entity type vw_facilitydepartmentrights is not part of the model for the current context.)

查看:81
本文介绍了使用ADO.NET实体模型时出错。 (实体类型vw_facilitydepartmentrights不是当前上下文的模型的一部分。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1个ado.net实体模式,其中包含2个视图。

当我在两个视图中应用where条件时,它给出了下面给出的错误。



实体类型vw_FacilityDepartmentRights不是当前上下文模型的一部分。



我尝试过:



belo是我的页面级代码

I have 1 ado.net entity mode having 2 views in that.
When I applied where condition in both views it gives me an error given below.

The entity type vw_FacilityDepartmentRights is not part of the model for the current context.

What I have tried:

belo is my page level code

<pre> 
var query = (from em in new Entities().EquipmentEntities
                              join ej in new Entities().vw_FacilityDepartmentRights
                              on em.DeptCharg2Key equals ej.DepartmentKey
                              where ej.EmployeeKey == Convert.ToInt32(Session["Login.EmployeeKey"])
                              where em.Cloaked == false
                              where em.Inactive == false
                              select em
                              );


Below is my context code
<pre>  protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<EquipmentEntity> EquipmentEntities { get; set; }
        public virtual DbSet<vw_FacilityDepartmentRights> vw_FacilityDepartmentRights { get; set; }







以下是模型上下文设计师的链接

2018-10-27_1422 [ ^ ]

推荐答案

Quote:

...不属于当前上下文的模型

"... is not part of the model for the current context"

from em in new Entities().EquipmentEntities
join ej in new Entities().vw_FacilityDepartmentRights



您正在尝试从两个不同的上下文实例加入实体。相反,您需要为查询创建单个上下文实例。


You're trying to join entities from two different context instances. Instead, you need to create a single context instance for your query.

var context = new Entitites();
var query = from em in context.EquipmentEntities
            join ej in context.vw_FacilityDepartmentRights
            on em.DeptCharg2Key equals ej.DepartmentKey
            where ej.EmployeeKey == Convert.ToInt32(Session["Login.EmployeeKey"])
            where em.Cloaked == false
            where em.Inactive == false
            select em;



如果可能,您应该使用块将上下文实例包装在中,以确保它被正确处理掉。但是,在代码使用块离开的范围之前,您需要完全显示查询结果。


If possible, you should wrap the context instance in a using block to ensure that it's disposed of properly. But you'll need to fully manifest your query results before your code leaves the scope of that using block.


这篇关于使用ADO.NET实体模型时出错。 (实体类型vw_facilitydepartmentrights不是当前上下文的模型的一部分。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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