为什么我得到错误实体类型 LAB_INVOICE_VIEW 不是当前上下文模型的一部分。?

why I got error The entity type LAB_INVOICE_VIEW is not part of the model for the current context.?

我正在使用 VS2015 并尝试将新的 table 添加到我的实体数据模型但是当我保存它时它删除了文件 context.cs 文件下的所有数据并显示错误所有模型在删除之前使用实体模型,

所以我尝试手动将模型添加到我的 DBcontext 我在这个 link 中找到了步骤:


我做了以下步骤:

1- 创建了数据库视图 LAB_INVOICE_VIEW

2- 创建模型

3- 手动将模型添加到我的 DBcontext 中:

public virtual DbSet<LAB_INVOICE_VIEW> LAB_INVOICE_VIEW { get; set; }

4- 也将配置添加到模型列下的模型中:

class MyTableConfiguration : EntityTypeConfiguration<LAB_INVOICE_VIEW>
    {
        public MyTableConfiguration()
        {
            ToTable("dbo.LAB_INVOICE_VIEW");
        }
    }

5- 将覆盖代码添加到 Dbcontext.cs :

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new MyTableConfiguration());
        }

6- 我将连接字符串从 EF 字符串更改为 ADO.NET 字符串,但出现错误:

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:



AljawdahNewSite.Models.Customers: : EntityType 'Customers' has no key defined. Define the key for this EntityType.

AljawdahNewSite.Models.Departments: : EntityType 'Departments' has no key defined. Define the key for this EntityType.

AljawdahNewSite.Models.Customers_Price_List: : EntityType 'Customers_Price_List' has no key defined. Define the key for this EntityType.

AljawdahNewSite.Models.LabTests: : EntityType 'LabTests' has no key defined. Define the key for this EntityType.

AljawdahNewSite.Models.Groups: : EntityType 'Groups' has no key defined. Define the key for this EntityType.

AljawdahNewSite.Models.Lab_Sample_status: : EntityType 'Lab_Sample_status' has no key defined. Define the key for this EntityType.

AljawdahNewSite.Models.Patients: : EntityType 'Patients' has no key defined. Define the key for this EntityType.
 

7- 我尝试了 link 中的解决方案,但仍然出现错误:


我需要你的帮助。

我删除了 EDMX 文件并重新创建了新的 EF 并添加了所有模型,这解决了我的问题。

谢谢格特先生