2

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

5
  • Just add the columns to your models, then in the Package Manager Console run add-migration and then update-database. add- migration will create a file with commands to create your new columns, update-database will apply the changes. Commented Jun 10, 2014 at 9:36
  • If it is the same column type, then you can have a base class with the column and derive all your models from it which will obviate the need for adding this column to every class.. Commented Jun 10, 2014 at 9:49
  • I think what you want is to add a column to many tables in a single go. Commented Jun 10, 2014 at 10:02
  • Yes that's it, Arijit. But regarding to the comments, it seems it's not appropriate to do so while using Code First migrations... Commented Jun 10, 2014 at 12:33
  • Thank you Muthu, that is exactly what I wanted. It looks more clean to me to make each of my models inherit some "Base Model" containing the columns I wanted to add to all of my database's tables. Commented Jun 10, 2014 at 12:56

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

3 Comments

Thank you for your answer ! But I actually wanted to know if it was possible to add these columns to all of my tables at once ? instead of adding properties to each of my models. If it's not possible, that is fine, since I only have about 10 models to update... but I was curious to know if there was a more "optimal" way of doing things.
In order to for the models to accurately represent your db tables, you have to add the properties to the models, that's just how it works.
Isn't it possible to mention a new class along with the db table name and the column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.