EF Code-First in an ecosystem of apps concerns - entity-framework

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?

Related

keeping database in sync ef core

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

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 Code First Library and Database Update Implications

We have recently begun using Entity Framework for accessing all the various databases we touch on a regular basis. We've established a collection of library projects, one for each of these. For many of them, we're accessing established databases that do not change, and using DB first works just great.
For some projects, though, we're developing evolving databases that are having new fields and tables added periodically. Because these libraries are used by multiple projects (at the moment, just two, but eventually many more), running a migration on the production database necessitates a republish of both/all sites that use that particular DB's library. Failure to update the library on any other site of course produces the error that the model backing the context has changed.
How can we effectively manage the deployment/update of the Code-First libraries to all of the sites that use them each time a change to the database is made?
A year later, here's what we came up with and have been using.
We now include the following line in the Application_Start() method:
Database.SetInitializer<EFLib.MyHousing.MyHousingMVCContext>(null);
This causes it not to throw a fit if the current database model doesn't exactly match what's in the code. While there is still potential for problems if non-backward-compatible changes are made, this allows for new functionality to be added without the need to re-deploy every site that uses these libraries when the affecting changes are not relevant to that particular site.

Migrating from Entity Framework Code First from Development to Production

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.

ASP.MVC2.0/EF4.0 site deployment/maintenance

My small team used asp.mvc 2.0/entity framework 4.0(model first approach)/Windows Server 2008r2/Sql Server 2008 r2 stack in out web site project. We've already complete developing process, and come to the web deployment stage. In this stage we are faced with the problem - ok we'll use vs2010 features for initial server/db deploy, but what we'll do in the future? Obviously some of our models can be modified after publishing in order to satisfy new conditions, and of course our server db will contains users data sets, articles etc. Is there any approach to update servers db with new db modification, without dropping db, and converting data from old instance to the new one?
Now we have found only DAC/DACPAC approach to update server db, but we don't know how to bind auto EF model generation with DAC.
May be there is exists another solution? Is there any standard way to resolve this kind of situation? Any advice?
Thanks
I'd be interested to know if you have found a solution to this yet?
Have you tried simply generating a database based on your EF model, and using a schema comparison tool such as SQL Compare to deploy changes from the EF-generated database and your target production server?