Mvc .net Defaultconnection To Azure Entity Framework

Posted on -

Apr 08, 2014  To deploy a Code First database to an Azure Web Site, you can use the Execute Code First Migrations check box in the Publish Web wizard: When you select that check box, Visual Studio configures the destination web site so that Entity Framework automatically deploys the database or updates it by running the MigrateDatabaseToLatestVersion initializer on application start. ASP.NET MVC–Azure Websites and EF Model First Connection Strings when Deploying from Github. One of the coolest things about Windows Azure Websites is the integration with source control, and the automated deployment hooks Azure offers. However, there are a number of small gotcha's related to deployment, chief among these is the protection of confidential configuration items such as database connection string.

  1. Mvc .net Defaultconnection To Azure Entity Frameworks

I’m trying to use Azure Key Vault for storing my web api connection strings for Entity Framework. Ideally I’d like to avoid coupling the key vault nuget packages with my data access code. My dbContext class has two constructors:

My code makes use of the parameterless constructor which gets the connection string from the web config. There are some places where I instantiate a new MyDbContext object, which prohibits a solution using injection.

The route I took is to set a static property on my dbcontext with a connection string locator:

My web api project has the nuget packages for retrieving the key vault secrets. So in my Global.asax file I have this:

I published this and it works, but it doesn't 'feel' right.

Another option would be to follow this article https://blog.falafel.com/keeping-secrets-with-azure-key-vault/ but it would require me to couple the KeyVault API packages with my data access.

I'm looking for feedback and direction. I should add that the reason I want to use key vault is because it will allow me to have azure administrators who can view the application settings online without having access to the sql database via connection string.

KeyVault resource with the new MSI implementation: https://github.com/Azure-Samples/app-service-msi-keyvault-dotnet/

Chad BengenChad Bengen

1 Answer

Here's how i resolved this, in case anyone else stumbles on it.

Created a ConfigurationManager class which first tries to get the value from the key vault, but on failure it uses WebConfigurationManager to read the app settings.

Then in my dbcontext class i call on Configurationmanager.GetConnectionStringValue('DefaultConnection').

Chad BengenChad Bengen
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged entity-frameworkazureazure-keyvaultazure-managed-identity or ask your own question.

-->

by Rick Anderson

Creating a Connection String and Working with SQL Server LocalDB

The MovieDBContext class you created handles the task of connecting to the database and mapping Movie objects to database records. One question you might ask, though, is how to specify which database it will connect to. You don't actually have to specify which database to use, Entity Framework will default to using LocalDB. In this section we'll explicitly add a connection string in the Web.config file of the application.

SQL Server Express LocalDB

Microsoft asp mvc

LocalDB is a lightweight version of the SQL Server Express Database Engine that starts on demand and runs in user mode. LocalDB runs in a special execution mode of SQL Server Express that enables you to work with databases as .mdf files. Typically, LocalDB database files are kept in the App_Data folder of a web project.

SQL Server Express is not recommended for use in production web applications. LocalDB in particular should not be used for production with a web application because it is not designed to work with IIS. However, a LocalDB database can be easily migrated to SQL Server or SQL Azure.

In Visual Studio 2017, LocalDB is installed by default with Visual Studio.

By default, the Entity Framework looks for a connection string named the same as the object context class (MovieDBContext for this project). For more information see SQL Server Connection Strings for ASP.NET Web Applications.

Open the application root Web.config file shown below. (Not the Web.config file in the Views folder.)

Find the <connectionStrings> element:

Add the following connection string to the <connectionStrings> element in the Web.config file.

The following example shows a portion of the Web.config file with the new connection string added:

The two connection strings are very similar. The first connection string is named DefaultConnection and is used for the membership database to control who can access the application. The connection string you've added specifies a LocalDB database named Movie.mdf located in the App_Data folder. We won't use the membership database in this tutorial, for more information on membership, authentication and security, see my tutorial Create an ASP.NET MVC app with auth and SQL DB and deploy to Azure App Service.

The name of the connection string must match the name of the DbContext class.

You don't actually need to add the MovieDBContext connection string. If you don't specify a connection string, Entity Framework will create a LocalDB database in the users directory with the fully qualified name of the DbContext class (in this case MvcMovie.Models.MovieDBContext). You can name the database anything you like, as long as it has the .MDF suffix. For example, we could name the database MyFilms.mdf.

Mvc .net Defaultconnection To Azure Entity Frameworks

Next, you'll build a new MoviesController class that you can use to display the movie data and allow users to create new movie listings.