Migrating from Entity Framework Code First from Development to Production - entity-framework

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.

Related

Is it possible to use Entity Framework Core with ODBC?

My company's main software application hasn't been updated in twenty years. I expect to soon be working on a complete rewrite of it. To that end, I am beginning to work my way through the book "Pro ASP.Net Core 3" by Adam Freeman (8th edition).
Our application was written to be independent of specific database types. Most of our customers use PostgreSQL, but a few use SQL Server. Therefore, we use ODBC because ODBC drivers exist for both of those databases, as well as several others. The application does not do anything fancy with the databases, and ODBC works well. We configure an ODBC DSN to talk to whichever database the customer has, and the application itself doesn't have to be changed.
A search on "Entity Framework Core ODBC" led me to the EF Core Github, where people have asked similar questions, and the answers were mostly along the lines of "why on earth would you want to do that?". Well, I need to do that.
Can I use EF Core with ODBC, or is there some other way that I can set up an Entity Framework Core application that does not have to be modified if the underlying database changes from PostgreSQL to SQL Server?
You could use your appsettings.json to store a value used to swap between the two. Those environment configs get reloaded on change (though you might have to restart your application to read them again, I'm not sure on that one).
Regardless something along the lines of this would suit your needs I think.
if (Configuration.GetSection("dbOptions")["postgres"]))
services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(Configuration.GetConnectionString("PostgresConnectionString")));
else
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnectionString")));
EDIT: I placed this in Startup.cs where you would normally configure the DBContext. I use a similar solution reading off the Environment type to load either the Prod or QA connection strings based on deployment. In principle, this should accomplish the same task without the need for rebuilding and redeploying the code base.

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 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.

How do I generate automatic migration scripts with model-first?

How do I generate automatic migration scripts with model-first in Entity Framework 4.3 or 5.0?
I've seen plenty of samples with this for code-first, but none that can be used in my scenario.
My problem is that I have a staging environment and a live environment. I may be making many model and database changes to the staging environment before publishing it all to the live environment.
This adds complexity when I then want to generate a migration script from my staging database to my live database.
Is this possible automatically somehow? Can this be achieved through the Entity Framework Power Tools?
Code First Migrations, as you would expect from the name, currently only work with a Code First model. There are other tools, however, that may prove useful for your scenario like SSDT's Schema Compare utility.

Incremental Database development in Entity framework code first

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