I am using VS2015 and tried to add new table to my entity data model but when I saved it its deleted all data under file context.cs file and showed errors all models used before deleted from entity model,
So I tried to manually add the Model to my DBcontext I found the steps in this link :
https://stackoverflow.com/questions/33426289/manually-create-model-for-single-table-in-entity-framework
I did the following steps :
1- created the database view LAB_INVOICE_VIEW
2- created the model
3- added manually the model to my DBcontext :
public virtual DbSet<LAB_INVOICE_VIEW> LAB_INVOICE_VIEW { get; set; }
4- added the configuration also to the model under the model columns :
class MyTableConfiguration : EntityTypeConfiguration<LAB_INVOICE_VIEW>
{
public MyTableConfiguration()
{
ToTable("dbo.LAB_INVOICE_VIEW");
}
}
5- added the override code to Dbcontext.cs :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new MyTableConfiguration());
}
6- I changed the connection string from EF string to ADO.NET string but I got the errors :
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- I tried the solutions in the link but still got the error :
https://stackoverflow.com/questions/20688922/the-entity-type-type-is-not-part-of-the-model-for-the-current-context
please I need your help .
CodePudding user response:
I deleted the EDMX file and created new EF again and added all models , This solved my issue .
Thank you Mr Gert