Migrations and configuration.seed not running on Azure deployment - entity-framework

I am on EF, with a standard code first DbContext.
public class MyContext : DbContext {}
My seed method in Configuration.cs runs fine locally, when I do an 'Update-Database'
But when I deploy to an Azure Web App instance, none of my seed data shows up. My initial migration runs, but when I add a new migration and publish, it does not get run. So basically only my Initial migration is showing up, and no seed data.
If I check 'Update database', the publish just hangs indefinitely.
I have read that there is supposed to be an 'Execute code first migrations' checkbox, but I don't seem to have that checkbox. I have tried cleaning my project, and restarting VS2013update4. Rebuilding the project, etc. Nothing makes it show up. I have also tried this: http://www.dominicstpierre.com/2012/11/enable-code-first-migrations-check-box.html, but it doesn't seem to work with VS2013u4.
Migrations are in my MyProject.DataLayer project. The actual web app is in MyProject.WebApp. Will this matter? It works fine locally.
What can I do to get this checkbox to show up?

Entity Framework sets up an initialiser on the target machine web.config
This initialiser is run when your app hits the database for the first time.
Here is a link to a good article on the asp.net site that explains how to achieve this: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

I had the same issue, the seed would simply not get executed after an azure deployment.
I fixed this by adding this to the entityFramework element in the web.config:
<entityFramework>
<!-- excluded other settings for brievity... -->
<contexts>
<context type="WebApplication.Data.ApplicationDbContext, WebApplication" disableDatabaseInitialization="false" >
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[WebApplication.Data.ApplicationDbContext, WebApplication], [WebApplication.Migrations.Configuration, WebApplication, Version=1.0.0.0, Culture=neutral]], EntityFramework" />
</context>
</contexts>
</entityFramework>
It is important that the namespaces are correct for ApplicationDbContext and Configuration, otherwise it will not work.

Related

InitialMigration empty even though there are data objects in Azure Service

I have been having trouble working with migrations and an Azure Sql Database for the past couple of weeks.
I run enable-migrations and a migrations folder with a congfiguration file is created.
I then run Add-Migration InitialCreate and a migration is created with empty Up() and Down() methods.
PM> Enable-Migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project MyService.
PM> Add-Migration InitialCreate
Scaffolding migration 'InitialCreate'.
The Designer Code for this migration file includes a snapshot of your
current Code First model. This snapshot is used to calculate the changes to
your model when you scaffold the next migration. If you make additional
changes to your model that you want to include in this migration, then you
can re-scaffold it by running 'Add-Migration InitialCreate' again.
I deleted an old database from my App Service and created a new one. This is now being targeted in my Web.config:
<add name="MS_TableConnectionString" connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MyService-20180212689875.mdf;Initial Catalog=aspnet-MyService-20180212578997;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
I have a feeling my code is targeting the old database which has been deleted on azure but holds the tables for the objects I'm now trying to add.
Update:
Removed the data objects and created a migration and it has been populated with the Delete code for non-existent tables(which existed in my last database). Definitely an issue with pointing to the right place. Any ideas?
Pretty new to Azure so not sure where to look to try and fix this.
Any help appreciated!
After adding migration, remember to enter "update-database" in Package Manager Console, which command runs the Up method to create the database and then it runs the Seed method to populate the database.
I have a feeling my code is targeting the old database which has been deleted on azure but holds the tables for the objects I'm now trying to add.
According to the code you provided, you set the database in Local in web.config and delete the database on Azure, but you could populate data to the deleted database on azure. There is no possible to achieve it.
Actually, when you publish you Web App to Azure, you could pick on the "Excute Code First Migrations" in Visual Studio and check if the TestContext is the database connection that you use now.
Each time you get both of them and after migrations, you will populate the data to the latest the database on Azure. You could read this article to know the details about Code First Migrations and Deployment.

Stop automatic migrations when I deploy to production

I have a linq to entities application that was working great until someone tried to add a column to a database table.
I was quite surprised to see that the production application crashed because it wanted to run to automatic migration and delete the column. It stopped due to potential dataloss.
How can I have my application run and not try to sync the database up? I would like older versions of my linq to entities code first applications to run alongside the newer versions if the database changes are not breaking changes.
In production, you could...
Use SetInitializer<YourContext>(null) on startup or...
Disable database initialization using the configuration file
<contexts>
<context type="Namespace.MyContext, MyAssembly"
disableDatabaseInitialization="true" />
</contexts>
Either way, automatic migrations would be disabled (as would the __MigrationsHistory/metadata check...I think).

Publish Web dialog doesn't detect my entity framework 5 context as Code First

I've recently upgraded a WFC project that uses Entity Framework from v4.3.1 to 5.0.
I'm running Coded migrations only (no automatic migrations).
Previously, I was using the Publish Profiles to deploy this solution and apply the migrations. Since upgrading the project to EF5, the migrations portion no longer works and the publish dialog doesn't detect the context as having code-first migrations.
Specifically, the .pubxmlfile changed from detecting my context as <Object Type="DbCodeFirst">to <Object type="DbDacFx"> which is incorrect for my context.
As a workaround, I've manually added the <entityFramework> database initializer configuration to my web.config transforms, but I'd like to understand why the publish profiles aren't working. That was a much nicer solution.
It once happened on me when merging another developer's commit and triggered Visual Studio project reload. That's how it caused the "DbCodeFirst" to "DbDacFx" change.
If I restart Visual Studio then everything goes back to what it should be.
Just another thought.
You probably missed adding the reference to EntityFramework into your project. By just adding the reference you should be able to control whether or not the DbCodeFirst option is available or not.
As this post suggests, try using the fully-qualified name of your DbContext as the name of the connection string. Instead of:
Web.config
<connectionStrings>
<add name="MyContext" .../>
</connectionStrings>
Use:
Web.config
<connectionStrings>
<add name="MyNamespace.AnotherNamespace.MyContext" .../>
</connectionStrings>
In my case, in order to use my existing publish profiles (.pubxml), I also had to manually edit the <ObjectGroup Name="..." ...>. Probably recreating the publish profiles would work too.
I had the same issue but not in the same context.
I had been using Code First Migrations with existing ASP.NET MVC 5.2.3 application using EF 6.1.3 for a month without issues. At one point in time I added support for Windows Azure Storage but I made some mistakes:
I added a new project. Unfortunately I chose "Console Application" instead of "Class Library". I tried to fix it by changing it back to "Class Library" in "Project Settings"
I used Nuget to Install-Package WindowsAzure.Storage but I installed it on the MVC project and not on the class library. I tried to fix it by doing Uninstall-Package on the MVC project and installing it to the correct project
I called the class in the class library "WorkOrderStorage"
I added the connectionString to <connectionStrings> element in web.config and a transformation in web.release.config
I guess my project was now in an inconsistent state. I noticed that it would forget about Code First Migrations (I monitored the changes to the .pubxml file):
when I put a reference between the MVC project and the library
containing the WorkOrderStorage class
when I created an empty 'WorkOrderStorage' class in one of the existing libraries
In the end I fixed it by recreating this library correctly from scratch as a class library (because of observation 1). I also named the class WorkOrderRepository (because of observation 2).

Entity Framework Code First Data Migrations not working with VS2012 Web Deploy

I have created an MVC 3.0 application using Visual Studio 2012, .NET 4.5 and Entity Framework 5.0.
Using Code First Data Migrations, I am able to correctly propagate model changes to my local test database, but I can't figure out how to get this to work when deploying to my staging and production servers using Web Deploy.
I have read the following article ...
http://msdn.microsoft.com/en-us/library/dd394698(v=vs.110)#dbdacfx
... which explains what's supposed to happen, but it's not working for me, as Web Deploy seems unable to detect that I am using Entity Framework. The tutorial shows a checkbox to enable execution of Code First Migrations ...
... but my dialog shows the only Update Database checkbox for each database.
I have read that, in order for Visual Studio to detect the use of an Entity Framework context, the Web.config must include an element that defines it. Here's mine:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<contexts>
<context type="MyContext, MyAssembly">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyContext, MyAssembly], [MyConfig, MyAssembly]], EntityFramework">
<parameters>
<parameter value="MyConnectionStringName"/>
</parameters>
</databaseInitializer>
</context>
</contexts>
</entityFramework>
Any suggestions would be greatly appreciated.
Thanks,
Tim
I have discovered the solution to this problem through experimentation.
In fact, my MVC application was created by VS2012 by converting from a VS2010 solution. Since the conversion process did not report any issues, I assumed that it had correctly converted everything, including the publishing profiles.
However, I discovered that the problem was in the conversion of these profiles and, unless I manually edit their XML files, there is apparently no way to get old imported profiles to participate in Code First Migrations.
Simply creating new publishing profiles in the converted solution results in the expected behavior.
Try this:
Enable-Migrations -Force
Ensure the below is set to true
AutomaticMigrationsEnabled = true;
Republish - worked for me

Entity Framework Execute Migrations on Test Server

I need to update a test server database when deploying, and I do not find a way to do it.
We are not using automatic migrations, so we do it manually.
Can I execute it on the test server directly? Maybe a console program?
I'm using the NuGet call for local development: Update-Database
I know we can generate the script files, do I have to do it manually?
You can use the MigrateDatabaseToLatestVersion database initializer to execute your migrations either through code or configuration. In code you'd do the following when the application starts:
Database.SetInitializer<MyContext>(
new MigrateDatabaseToLatestVersion<MyContext, MyMigrationConfig>()
);
Or in your app's .config file:
<entityFramework>
<contexts>
<context type="MyContext">
<databaseInitializer
type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[
[MyContext], [MyMigrationConfig]
], EntityFramework" />
</context>
</contexts>
</entityFramework>
If your using Visual Studio, you can use the publish fonction to also execute a script. Look here for more détails:
http://learn.iis.net/page.aspx/1081/building-a-web-deploy-package-from-visual-studio-2010/
If your using team fondation server, you can even automate it:
http://weblogs.asp.net/jdanforth/archive/2010/04/24/package-and-publish-web-sites-with-tfs-2010-build-server.aspx
Also to generate the script maybe you could use migrate.exe (it comes with ef 4.3 I think when getting the nuget package) and run it in a post build évent...