我正在使用 VS2015 并嘗試將新表添加到我的物體資料模型中,但是當我保存它時,它洗掉了檔案 context.cs 檔案下的所有資料,并顯示了從物體模型中洗掉之前使用的所有模型的錯誤,
所以我嘗試手動將模型添加到我的 DBcontext 我在這個鏈接中找到了步驟:
https://stackoverflow.com/questions/33426289/manually-create-model-for-single-table-in-entity-framework
我做了以下步驟:
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- 我嘗試了鏈接中的解決方案,但仍然出現錯誤:
https://stackoverflow.com/questions/20688922/the-entity-type-type-is-not-part-of-the-model-for-the-current-context
拜托我需要你的幫忙 。
uj5u.com熱心網友回復:
我洗掉了 EDMX 檔案并再次創建了新的 EF 并添加了所有模型,這解決了我的問題。
謝謝格特先生
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/381538.html
標籤:C# asp.net-mvc 实体框架 数据库上下文
