为什么 LinqPad return The name 'ModelName' does not exist in the current context 错误?

Why LinqPad return The name 'ModelName' does not exist in the current context error?

我有一个 Asp.net MVC - C# 项目,想使用 LINQPad 来测试一些 Entity Framework在项目中使用它们之前的声明。

该项目有不同的 class 库,例如

A) 我在 [=44= 中的 DAL 添加了一个 EF 连接 ]LINQPad 并在其 window 中成功加载了左侧树中的所有实体。 [见下图]

B) 我还从 LINQPad Query 添加了一些必需的引用到以下 DLL:


但是»

当我右键单击左侧树中的一个实体并 select 每个选项时,它显示此错误:

The name '[Conceptual_Model_Name]' does not exist in the current context

为什么? 我该如何解决这个问题?


编辑 1:

OnModelCreating 方法中 ApplicationDbContext class 我调用以下方法 添加实体到 DbContext动态

    public void LoadEntities(Assembly asm, DbModelBuilder modelBuilder, string nameSpace)
    {
        var entityTypes = asm.GetTypes()
            .Where(type => type.BaseType != null &&
                           type.Namespace == nameSpace &&
                           type.BaseType == null)
            .ToList();

        var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
        entityTypes.ForEach(type => entityMethod.MakeGenericMethod(type).Invoke(modelBuilder, new object[] { }));
    }

我用这行代码调用它:

LoadEntities(typeof(AppUser).GetTypeInfo().Assembly, modelBuilder, "DomainClasses.Entities");

是否可能与动态加载模型有关?

我自己解决了!

我在问题中写的怀疑是正确的。正如我在问题中所写,我将实体添加到 DbContext 作为 动态 但现在我将其更改为 静态方式 像这样:

// Add this property to the ApplicationDbContext class

public virtual DbSet<Consumption> Consumptions { get; set; }

现在,当我在 LINQPad 中使用 Consumptions 属性 时,它起作用了...