Deploy Entity Framework Code First - entity-framework

I guess I should have thought of this before I started my project but I have successfully built and tested a mini application using the code-first approach and I am ready to deploy it to a production web server.
I have moved the folder to my staging server and everything works well. I am just curious if there is a suggested deployment strategy?
If I make a change to the application I don't want to lose all the data if the application is restarted.
Should I just generate the DB scripts from the code-first project and then move it to my server that way?
Any tips and guide links would be useful.
Thanks.

Actually database initializer is only for development. Deploying such code to production is the best way to get some troubles. Code-first currently doesn't have any approach for database evolution so you must manually build change scripts to your database after new version. The easiest approach is using Database tools in VS Studio 2010 Premium and Ultimate. If you will have a database with the old schema and a database with the new schema and VS will prepare change script for you.

Here are the steps I follow.
Comment out any Initialization strategy I'm using.
Generate the database scripts for schema + data for all the tables EXCEPT the EdmMetadata table and run them on the web server. (Of course, if it's a production server, BE CAREFUL about this step. In my case, during development, the data in production and development are identical.)
Commit my solution to subversion which then triggers TeamCity to build, test, and deploy to the web server (of course, you will have your own method for this step, but somehow deploy the website to the web server).
You're all done!
The Initializer and the EdmMetadata tables are needed for development only.

Related

Workflow for updating latest Entity Framework Core migrations automatically

So I have an EF Core project. When I need to add/migrations I issue and update command:
dotnet ef database update -v
This all works fine, if I need to update the QA, Staging, Prod database I update the connection string in my appsettings.json file and run the command.
What happens if I don't have access to production from my local machine? How do you go about updating the database to the latest migration?
If I remember working with Entity Framework (not .net core). It would try and update the database automatically when I deploy a new version of the .net application. Does this functionality still exist in .NET core?
Yes you could run
dbContext.Database.Migrate();
On startup but it is generally not a good idea to conflate database migration with your application lifecycle - best practice is to keep your application start up as fast and simple as possible because an application start failure is hard to diagnose remotely, and a migration would introduce a lot of unwelcome complexity.
The alternative is to run a migration operation as part of your deployment. It depends on what method you use to deploy but say for example you use a CI server, you will be able to run
dotnet ef database update
after copying the new code but before starting the application back up.

Alternatives to SQLite in VS2013 with EF6 and designer support

I have previously used System.Data.Sqlite 1.0.85 with EF5 in VS2010 and it worked well and I came to really like the designer. On my new machine, I've switched to VS2013 Community and tried to get Sqlite 1.0.94 and EF6 running, but it's such a hassle and I'm starting to get really frustrated. I can't seem to get it running on my old project, which I need to work on, but only on a fresh one. Tried copying the old stuff, but it just stops working again. From what I've read, I'm not the only one having these issues.
I can connect to the database in the server explorer, but the connection won't show up in the Entity Data Model wizard. It did run once on a fresh project, but since I keep running into trouble, I'd rather switch to something else that actually works.
So my question is, what alternatives are there? It's for a small business application. Would LocalDb be an alternative? Are there any other good databases/providers (free ones) that work well with EF and have designer support in VS2013?
Thanks for any feedback!
LocalDb can be used in the dev environment and works really well. But it might be too limiting for production use depending upon your requirements. Another option would be to use SQL Server Express. But this again might have limitations in a prod environment. The following link gives you an overview of the features of different versions of SQL Server:
SQL Server Editions
Another option would be to go for something like PostgresSQL or MySQL but I haven't used them, so can't say much about them.

EF6 code-first migrations: how to deploy to staging environment the right way?

I have been following a number of tutorials regarding code-first migrations and am now at the stage where I am ready to deploy to our staging server.
We normally publish web apps to the filesystem and then manually update sites through Remote Desktop (not the greatest I know).
All the tutorials and best practises as far as code first and deployment go seem to be either out of date of specific to Azure deployment.
What is the current best practise for deploying a web app that has been developed with code first migrations (EF6) to a live environment? How then are updates to the live environment handled?
I understand that I can generate scripts using Update-Database but then these do not include any Seed Data. Are scripts the way to go?
Thanks,
You can use DBMigrator update menhod - this will run any pending migrations. The Seed method in your Configuration class will run every time your application starts.
You can also use migrate.exe to run database updates.

SQL Server Data Tools and Edmx

So we're using the new SSDT Microsoft released, pretty cool stuff. We are keeping a database project under version control with all the schemas, and an offline database for development and we can later deploy on SQL Azure database. We;re using EF in development, so my question is where would the edmx fit in, should we update the edmx file from the offline database or from the online SQL Azure directly, whats the best practice on this?
I would say that in your case "the production database is the truth", so I would update from SQL Azure. There's no right answer tho really.
Incidentally, in the early betas of SSDT it was possible to have a reference from an EDMX to a SSDT project thus your source code became the truth (which, in my opinion, is preferable) and the EDMX knew it was always working against "the truth". Unfortunately they ditched this feature and there are no signs of it returning.
For the EF to work correctly the EDMX file has to be in-synch with the database you are connecting to. It's hard to answer your question without knowing the development process you follow but I would imagine you use Sql Azure in production and develop against an on-premises database. Therefore one copy of the Edmx file will be used on production server. In the development environment you have a "living" copy of the edmx file that is changed as needed when the local database changes. When you get to the point you when you are ready to ship you deploy your app (include the edmx file) to a production environment that uses Sql Azure.
If, in your development environment, you update the edmx file from the SQL Azure then stuff will break or will not work correctly if the schema of the database in Azure is different from schema of your local database.

Deployment of ASP.NET MVC 4 web application with Entity Framework Code First approach

I want to make incremental releases of a web application implemented in ASP.NET using MVC 4 and Entity Framework Code-First.
I want to make a good and safe deployment routine. I currently use Subversion as source control, and plan to make releasable tags available on the SVN server, to deploy on the production server. The server runs on a separate network, but has access to the SVN server.
The server runs both IIS and MSSQL server.
What is the best way to deploy new releases to the production server using SVN? Prebuilt binaries or build on site.
How to handle model changes on the production server? On development I use Update-Database function on the Package Manager Consol in Visual Studio.
EDIT:
I will try to base my setup on this blog post, which uses git instead of SVN: http://www.jayway.com/2012/10/17/a-simple-continuous-integration-and-deployment-workflow-with-asp-net-mvc-github-and-jenkins/
But still all suggestions are very welcome.