Centro Assistenza di Register.it

  1. Assistenza
  2. Hosting
  3. Archivio Hosting
  4. Hosting Windows
  5. How to fix error when you publish a web application with Entity Framework and .Net

How to fix error when you publish a web application with Entity Framework and .Net

Problem
If a customer tries to publish a web application with .NET – Entity Framework, that use SQL 2016, it will show this error:
System.Data.SqlClient.SqlException: CREATE DATABASE permission denied in database 'master'.

 Why this happen

Our Hosting is a Shared Hosting, so every part of Environment has a Security Contest that not permit a customer to see content or databases of another customer.

In this specific case, every SQL Login have a specific DENY in the permission VIEW ANY DATABASE

This DENY not permit to see other db except master and tempdb.

So when you try to start application, Entityframework do a query to table sysdatabases to see if db exist, can’t see db, so try to recreate it from Zero (our DBA permission doesn’t permit to create a new db), web page show error.

This didn’t happen with SQL 2014 because there was a bug where a Login with DENY in VIEW ANY DATABASE, can see metadata of all DATABASES in table sysdatabases of db Master

This bug (that was there from SQL 2005, in the years has become a feature) is fixed in SQL 2016.

Solution

To use EntityFramework in our Shared Hosting, you have to do:

  • Create db from control panel
  • Create all db structure with SQL script by MylittleAdmin panel or restore db from a backup
  • Change your web.config to turn off db initializer in <contexts> and <appSettings> sections, example:
<contexts>
<context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
   <!--<databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" />-->
   </context>
</contexts>
<appSettings>
<add key="DatabaseInitializerForType ContosoUniversity.DAL.SchoolContext, ContosoUniversity" value="Disabled" />
</appSettings>

Example Guide Site:
http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx

In this case when application starts, it will not check if the db exists but will assume that there is.

Workaround
Remove DENY in VIEW ANY DATABASE permission in the SQL Login.
We can't apply this in a shared hosting enviroment.

 


Questo articolo è stato utile?

Non hai trovato quello che cerchi?

Contatta i nostri esperti, sono a tua disposizione.

Contattaci