ASP MVC - Adding columns to all tables using EF Code First

asp.net-mvc-5 c# ef-migrations entity-framework-6

Question

I need to add a set of columns to all of my database's tables (SQL Server).
I have generated my database using Code First migrations in Entity Framework 6.

Is it possible to add all these columns to all of my tables at once ? For instance, by overriding the OnModelCreating method with some specific code ?
Or do I have to add these columns to each of my Models used to update the database ?

Thank you in advance for your help !

--
Glad

1
2
6/10/2014 9:58:22 AM

Accepted Answer

I found the solution, thanks to Muthu.

To automatically add those "common columns" to all my tables, I just had to create a "Base Model" containing these columns, and make all of my models inherit from it.

This avoids repeatedly adding the columns to each Model as properties, and looks cleaner I believe.

2
6/11/2014 7:22:18 AM

Popular Answer

I suggest to do it like this:

  1. Add these columns as properties to model classes. That's the main point of using an ORM framework such as EF.
  2. Execute "Add-Migration NameForYourMigration" in Package Manager Console. This will add a new migration class to your project.
  3. Execute "Update-Database" in Package Manager Console. This will update database based on instructions in migration class that you have just created.


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow