Incremental Database development in Entity framework code first - entity-framework

How can I do incremental developments with entity framework code first database. Because if I change something i model classes it will regenerate the database which cased to loss my data already in the database. I'm using DropCreateDatabaseIfModelChanges . Is there any thing other than that to execute alter quires rather than recreating.

EF Code First Migrations would help you here, it's in alpha/CTP currently: Entity Framework Code First Migrations: Alpha, also check out the ADO.NET team blog:
The most consistent request we have heard from you since releasing EF
4.1 has been for a migrations solution for Code First that will
incrementally evolve the database schema as you model changes over
time. Today we are announcing the release of our first Community
Technical Preview (CTP) of our Code First Migrations work.

As I recall, the Microsoft docs tell you to be sure not to use DropCreateDatabaseIfModelChanges in production environments. The point of that option is to help you come up with code-based data population for your test runs. I haven't seen any tools to help with incremental changes when using code-first. Where I work, we use a database-first setup, and we create a change script for each new release that includes alter and insert statements.

incremental database development is not currently available in the current version of the codefirst framework however it is included in the roadmap for the next release which will ship with MVC 4
as of right now you would need to remove metadata tracking from the database conventions and update the database manually via scripting or using the sql tooling until this new convention is added to the framework

Related

Core 2 Keep domain models in sync with database changes

Need help! I am new to Core 2.0 and VS 2017 and haven't had much experience with MVC. I have an existing database that I need to use with a core 2.0 project at work. I was able to use the Scaffold-DbContect to initially create the domain models from the existing database.
However, the database developers are making changes to the database and adding new tables. I need to keep my domain models in sync with the database changes that are being made.
The only thing I can find on the internet is how to make changes to the model and update the database schema. However, I need to update the model from changes made to the database.
EF Core works on Code First approach. And You guys are following DB First approach together. So you should make changes in your code and then generate migrations accordingly, Otherwise, it will lead you in trouble.
You can use EF Core Power tool for generating the db changes at code side. But in this case you have to take care while generating migrations from code side.

Entity Framework Explicit Migrations

we have a project running Entity Framework 6.1 and we started using explicit migrations a while ago (in the past we would use automatic migrations) and we have run into the following situation.
I create an explicit migration to create some indexes on fields. I do this in a seperate branch.
A colleague of mine also starts an explicit migration to do some other work in his own branch.
Each branch is code reviewed and merged into the master branch when approved.
But now we noticed that my explicit migration to create the indexes, was created on a different version of the model. Since it's a project with multiple developers, the model is always changing. So if we check what SQL code would be generated to update the database, we see that new columns/tables/... that have been added in the meantime while I was working on my branch are being dropped, that my indexes are then created and afterwards those columns would be added again.
How can we avoid this? What are we doing wrong in our workflow?
With EF 6 each migration has metadata about last state of DB.
In EFCore this is much better done with separate file that has snapshot of DB.
Here are some good practices for Migrations in Team Environments:
https://msdn.microsoft.com/en-us/library/dn481501(v=vs.113).aspx
How to manage Migrations in a project with multiple branches?
Now your situation is pretty specific and I am not sure that any of these procedures has automatic solution for it.
One way I can think of is to have DB model not locally but on a server and that each developer targets that when creating migration.
However in the previous blogs shared DB was not considered best practice.
You would need to figure out some hybrid procedure to comply with every advice.
Good luck...

Entity Framework database migration

I am working with sql server and entity framework in a web ASP.Net C# Project. I am working with "Database first" concept. This mean i draw my database structure from sql server management studio on my local development computer. I add fields, rename fields, add table, change type, etc in the life of my project.
What i want to do is to see what to do when i want to apply database structure changes on my production(s) server(s). Is there a way for entity framework to "detect" changes with a concept of migration versioning like in symphony doctrine ? I actually patch by hand by applying sql scripts on my production server.
Thanks
In all cases you need to do some development tasks
Depends on the entity framework model you are using,
Code first approach : then you can use the reverse engineering, you can find this extension online, you can use the Tools > Extensions and Updates to find it , or you can update your classes manually.
Model first approach: then right click inside the edmx and Update Model from database

Entity Framework Database Generation Power Pack?

I use Model-first with EF, and I want to have an automated gap DDL script when I change my model. With "Entity Framework Database Generation Power Pack" We had it in past, but I read that was not supported in VS2012.
Any changes about that?
For Who dont't understand this need, I would like to remmember that in production enviroments, development team dosen't have access to DB. We must create and send to production Support team, DDL deployment scripts that preserve data and all DB without any recreation.
You should have a look at Database.SetInitializer, which mainly determines what happens if there is no database present when the application is started for the first time, and migrations which can be used to update the datebase when a new application version (which requires an updated database) has been deployed. If the built-in support for migrations data aren't enough, you also have the ability to add raw SQL data to handle migrating to a new version.

Migrate a given (old) database to current model

I am struggling with entity framework's migration quite a bit. Below is how I plan on using migrations, but I find no information on how to accomplish this:
My users are creating databases (Sql Compact Server) on their premises with different versions of my software. Each version introduces a slightly changed EF model. As soon as a user updates to a newer version of the software and opens a database with a previous version of the model, I would like to have some sort of "auto migration" bring the given database up-to-date to the current model.
Is there any way to do this?
Use the MigrateDatabaseToLatestVersion database initializer. See this link for more info.