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...
Related
I have read multiple posts about this but do not have a clear answer yet.
We are transitioning to EF Core 2.0 company-wide, one project at a time.
The challenge is this:
A new project starts and a database is created using code first, migrations etc.
another programmer needs to create a project targeting the same database.
This programmer can use Scaffold-DbContext and generate current models.
However, a new column is needed and this second programmer adds it.
Now...how do we best update the other projects?
Is there something that checks and syncs or shows what is out of sync between your model and a database? Meaning check the database for changes...not the model.
We don't mind buying a tool if that is the best solution.
The solution we have been using, very successfully is the Database project in Visual Studio.
Using that each developer has the project in their solution, changes are made against it locally.
Then we can do a "Schema Compare" inside of VS.
We have been using this successfully for 4 of us the past three weeks extensively with almost no issues.
This has even worked for keeping versions and changes to our stored procedures current.
It works well with VSTS also.
Here are some of the posts I read that helped me understand it:
https://www.sqlchick.com/entries/2016/1/10/why-you-should-use-a-ssdt-database-project-for-your-data-warehouse
https://weblogs.asp.net/gunnarpeipman/using-visual-studio-database-projects-in-real-life
..and this forum had a lot of relevant questions/answers:
https://social.msdn.microsoft.com/Forums/sqlserver/en-us/home?forum=ssdt
I have a concern that I would like some input on to see if there is a solution. We have an ecosystem of about 30 web applications that all connect to the same database. We are going to be updating the applications and with that we are going to be moving to a new database schema. I have a process that will be pulling all the old data into the new schema using EF Code-First for the new schema.
Well, today I ran into an issue when two branches of the solution that I wrote to migrate the data have a slightly different schema (one branch put a MaxLength on a field and an index on another). The issue that I had was that the database was out of sync in one branch, but up-to-date in the other and the branch that was out of sync would not run until I ran Update-Database.
I was thinking about putting the code-first poco classes into a library along with the DataContext to be able to be used by the various web apps. I would then make this library available to the team using an internal NuGet server.
My concern is that if the schema changes (doesn't happen very often, but it does happen) what happens to all of the web applications that are relying on this library for data connectivity? Would they all break (my assumption is that they would)? If so, all of the production web apps would be down. Is there a way to get around this?
I have been working on a side project for the last few weeks, and built the system with EntityFramework Code first. This was very handy during development, as any changes i needed to make to the code were reflected in the DB nice and easily. But now that i want to launch the site, but continue development, i dont want to have to drop and recreate the DB every time i make a tweak to a model...
Is there a way to get EF to generate change scripts for the model change so i can deploy them myself to the production server? And how do i use the database somewhere else (Windows Service in the background of the site) without having to drop and recreate the table, and use the same model as I have already? Kind of like a "Code first, but now i have a production DB, dont break it..."
Personally i use the builtin data tools in VS2010 to do a database schema synchronization for updating production.
Another cheaper tool if you dont have VS Premium is SQLDelta which ive used in the past and is really good.
Both tools connect to the two database versions and allow you to synchronise the table schemas first. Both also have an export to SQL script functionality.
Comming up for EF is Migrations which allows you to solve just this problem within your solution however its still in beta. Migrations lets you describe upgrade and downgrade events for your database in code.
No RTM version of EF has this feature. Once you go to production you must handle it yourselves. The common way is to turn off database initializer in production and use some tool like VS Premium or RedGate Database compare to compare your production and dev database and create change SQL script.
You can also try to use EF Migrations which is exactly the tool you are asking for. The problem is it is still beta (but it should be part of EF 4.3 once completed) so it doesn't have to work in all cases and functionality / API can change in RTM.
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
I've been working Entity Framework trying to get better with it. I'm liking what I'm seeing thus far but now have a question. With this new 'Code First' approach (from the CTP 4 download) we can now use EF from a code first approach, but I'm trying to find out if one can use an existing EDMX file with this approach.
I have a project I'm working on which has an EDMX file and I notice the ModelBuilder has a RegisterEdmx method but am not finding a lot out there on whether this will allow to use an existing EDMX file with my code first approach.
Also, I know with this new CTP things like RecreateDatabaseIfModelChanges are avilable but these options drop the database and recreate it, wont this cause all your data to be lost if you ever change your models? Is there something I'm missing here?
I can't speak to using previously generated EDMX files but there is support to use Code First with existing databases. As for the automatic Recreate, yes, this will kill all your data. This is meant only in rapid development where the persistence of data doesn't matter (and, in fact, is likely unwanted as you discover issues with business logic and want a clean start with your updates.)
This is meant only as a quick way to develop. As of (when Scott Guthrie blogged about the CTP - jump to section 5) there are no data migration features available. Your options are to manually update the database to match your model, delete the database and let it be recreated or set the automatic recreate option. Only the first option is non-destructive to your data.