简体   繁体   中英

ASP.NET MVC 4 EF6 cannot connect to SQL Server Express database

I have a weird error when I move to SQL Server Express from LocalDb. This is the error:

This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.

This error occurs when I tried to CreateDatabaseIfNotExists on context initialization.

Here is my connection string:

<add name="DefaultConnection" 
     connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=AlvinCMS;MultipleActiveResultSets=True;" 
     providerName="System.Data.SqlClient" />

Here is my context:

public class AlvinCMSMigrationDBContext : DbContext
{
    public AlvinCMSMigrationDBContext() : base("DefaultConnection")
    {
        Database.SetInitializer<AlvinCMSMigrationDBContext>(new CreateDatabaseIfNotExists<AlvinCMSMigrationDBContext>());
    }

I initiate the database on the global.asax :

protected void Application_Start()
{ 
    //Check And Init Database
    Alvin_CMS.App_Start.DatabaseConfig.Initialize();

And finally this is my initialization code:

public static void Initialize()
{
    Alvin_CMS.Models.AlvinCMSMigrationDBContext migrationDB = new Models.AlvinCMSMigrationDBContext();

    try
    {
        if (!migrationDB.Database.Exists())
        {
            migrationDB.Database.Initialize(false); //ERROR HERE!

            AlvinCMSExtension.Models.AccountDBContext accountDB = new AlvinCMSExtension.Models.AccountDBContext();
            accountDB.Database.Initialize(false);

            SetDefaultValue(migrationDB);
        }

        migrationDB.Database.Initialize(false);
    }
    catch (Exception e)
    {
        migrationDB.Database.Delete();
        AlvinCMSExtension.Helper.Log(e);
    }
}

I do not know what is wrong with my code, because it was fine when I use LocalDb, but now I cannot even connect to my database.

Here are my attempts to fix this issue:

  1. I put Persist Security Info = True;Trusted_Connection=False; - still producing the same error.

  2. I tried to change Database.SetInitializer<AlvinCMSMigrationDBContext>(null); But I need to create the database if not exist, so this is not a fix for me

You can use connection string as shown below.

<add name="DefaultConnection" connectionString="Server=localhost; Database= 
AlvinCMS;Trusted_Connection=True;" providerName="System.Data.SqlClient" />

In my case "StoreMan" is the database name. Replace it with your own.

<add name="StoreMan" providerName="System.Data.SqlClient" connectionString="Server=localhost; Database=StoreMan;Trusted_Connection=True;Integrated Security=True;MultipleActiveResultSets=True"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM