Search code examples
c#entity-frameworkcode-first

Including a new table in the DBContext made with CodeFirst


We have a Data library they have built it with CodeFirst. Now I ran a SQL command and added a new table to that database. But Now I want to also see its generated object class, its DBContext definitions ,etc.. in the code first code so I can use them in my LINQ queries. So what I did was following number 3 method from this MSDN page: https://msdn.microsoft.com/en-us/library/jj200620.aspx

But it didn't do it right. For example it did not add any definition for this new table to DBContext.cs file, for example all my other tables that used to be there are defined like this:

DbSet<Zipcode> Zipcodes { get; set; } 

But it has not added anything for me.

What is the correct way to do this?


Solution

  • I'm unaware of a way to simply add a new table and have it 'plug-n-play' with your existing model without manual work. The two options I know of are:

    1. Rebuild the model using Code First from DB and include your added table
    2. Manually create the table as a class and add the DbSet and entity in the OnModelCreating method in your model