Running DNX (EF7) database migration on Azure - asp.net

I've been running a MVC project with ASP.NET 4.6 and EF7 locally and everything works fine. I ran the dnx . ef migration Initial commands to create the database tables, and everything ran fine. The app works.
With Azure I have one problem, I can't seem to run the dnx . ef migration command so my SQL database is empty. I've debugged Startup.cs and the connection string is correctly retrieved, but the tables are not there.
I used the Publish option in Visual Studio 2015 to deploy to my Azure Web App.
How can I run this command on my web app? Is there another way to generate the database?
Thanks

It seems that the EF team has eliminated the infamous database initializers and offers a more versatile method for that on version 7.
Just use these methods in the pipeline before any call to the database,
yourDbContext.Database.EnsureCreated();
to create the database in case it doesn't exists and
yourDbContext.Database.Migrate();
to apply migrations (if you use this feature later). Both methods above also have an asynchronous version.
In my solution I have created an static class that I use to initialize the database and seed it with some data. I call the Seed method from the Configure method in the Startup class when some condition is met.

Related

ASP.Net Core dotnet ef migration not creating Identity tables on server, while other custom tables are created

I have a ASP.Net Core 2.2 C# application, using the Core Identity. I'm using code first, so when there are database-related changes in the code I use the dotnet ef migrations & update commands. I have extended the standard Core Identity and have added other custom tables, created in the C# code. Locally all the migrations worked perfect and everything is working fine.
Now I need to transfer the complete application to a server. I can't get the SQL database to create the Identity tables, using migrations & update. My other custom tables are created as expected.
This is what I tried:
1. Copy the complete project to the server
2. Create the needed SQL database instance
3. In Command-line execute: dotnet ef database update ==> Only custom tables where added, NO Identity tables
4. Remove all the migration files from the project
5. In Comamand-line execute: dotnet ef migrations add ServerCreate ==> In script creating of custom tables if visible, NO Identity tables
6. Just tried to "dotnet ef database update", but as expected NO Identity tables
Locally everything is working fine. How can I create a clean database code first with all the needed tables?
Any help would be great, I'm clueless .....

Deploying ASP.NET Core solution with class library to Azure

I'm working in VS2015 and have a ASP.NET Core solution with two projects - an API Web Project and a Class Library that holds all the data entities, context and Entity Framework migrations. The API project references the class library and all works well on my local machine.
I now want to deploy the solution to Azure and this is where I'm hitting the problem. If I right click on the API project and go through the Azure App Service publish wizard, on the Settings tab I expand Databases and the message is "No databases found for this project" - which I'm guessing is because it can't find a context as it's not in this project.
If I do the same on the CL project though, there is no Azure App Service deployment option, the only option is File System and clearly there's no option to create the database there either.
So, in summary, my question is how I can deploy this type of solution to Azure and have the database created and migrations applied?
I think you need to create the DB first in the azure and then try to publish your application through the wizard. The database is on your local machine and the application will work just fine on your local environment. But on the cloud you have to first create the database on Azure SQL. Then you need to get the SQL connection string from the portal and update your config file accordingly. Once this is done you can then publish your application from Visual Studio. Please note that the wizard will still not show you the databases, but the application, when configured properly will run fine.

Deploying asp.net app with automatic migrations enabbled

I have automatic migration enabled in my asp.net 4.6 mvc 5 web app. I added a custom field to IdentityUser model and when I run the web app I get
The model backing the 'ApplicationDbContext' context has changed since
the database was created. Consider using Code First Migrations to
update the database
I figured that I have to ran Update-Database from Package Manager Console and that resolved it locally in my dev environment. Now, when I deploy the app to an upstream environment (e.g. Azure) how do I trigger updating that database?
Try adding the below code to the Global.asax
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());

Stop application if db version is wrong

I have an web application that runs in JBoss and i use FlyWay in command line mode to migrate between database versions.
I want integrate Flyway in to my application using Spring Framework and only stop the deployment in to the web server if the db version is wrong. Is this possible?
First of all, I strongly recommend migrating the database on application startup so a mismatch can never occur.
If this is not an option, you can implement custom logic to check the results of Flyway.info() against the desired version.

Entity Framework Code first + migrations and creating a new database

I am using VS2012, framework 4.5, and Entity Framework 5.0 using code first and migrations.
I published my site using web deploy in which I changed the connection string to a new sql server 2008 db and turned on Execute Code First Migrations.
This all worked and the database tables where created but the seed method wasn't called.
Any ideas why the seed method wasn't called?
Well I found out my issue through trial and error. It looks like in order to get the code first migrations to work in the project properties you need to turn on "Include all databases configured in Package/Publish SQL tab" and then go to those settings and ensure your connection string is listed and check in the Database Entries table.
This wasn't an obvious answer to me because it looked like code first migrations were handled using web deploy settings in the publish profile so having to do it in both places doesn't seem right.

Resources