Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using code-first approach of Entity Framework for my project. I have already created many tables and those tables contain data too.

Now, I am trying to add few columns in few tables. One way is to delete the database or tables and re-create it. But, I hope this process is not a good one as because everytime I need to change the table structure, I will have to delete the whole db.

Is there any other way out? Instead of deleting the whole database or tables can we modify the table structure using code-first conventions?

Thanks
Posted

1 solution

Google for "Entity Framework Code First Migrations".

It's not that hard but you do have to be careful and understand each step in the process, otherwise you might just get yourself into a position where you will be rebuilding your database from scratch.

1) Make your changes to the entity model.

2) Compile the code.

3) Add-Migration.

4) Compile the code again.

5) Update-Database.
 
Share this answer
 
Comments
Member 11072126 8-Aug-15 13:28pm    
Thanks for the reply.
This one I saw and its saying that I will have to install 'migration package' separately (which I do not want) and update the database using visual studio command prompt.
Is there any other way out? I mean is there any trick which I can use in the context class to update the table instead of installing 'Migration Package'?
Dave Kreskowiak 8-Aug-15 13:35pm    
No, you don't have to install some "migration package" separately. Migrations are built into Entity Framework.

Make sure you're reading something recent and for Entity Framework 6 and not EF 4.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900