Error logging into EF6 migration database (LocalDb)

VS 2013 MVC5 first project.

Im working through ASP.NET Getting started with EF6 using MVC5 to speed up the implementation of recent changes, and I had a problem with migrations. In the first module, the database is created using the database initializer:

<contexts>
      <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
        <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" />
      </context>
</contexts>

and this connection string:

<connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>

It works great. In the Primary Migration and Code Deployment module, migrations are configured. The initializer is commented out, and the database name is changed to ContosoUniversity2:

<connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>

When update-databaselaunched from the Package Manager Console, it crashes with an error message:

Cannot open database "ContosoUniversity2" requested by the login. The login failed.
Login failed for user 'MyMachine\MyUser'.

Ive . Initial Catalog AttachDbFilename=|DataDirectory|\ContosoUniversity2; update-database ( DB App-Data , ):

<connectionStrings>
    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0; AttachDbFilename=|DataDirectory|\ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>

Initial Catalog. connectionString Web.Release.Config, .

, , Initial Catalog update-database ?

1:

, LocalDb

MyUser SQLExpress (sysadmin). SSMS MyUser DBs. . , MyUser, , databaseInitialzer. VS Admin, databaseInitialzer update-database . DBs Admin's MyUser's, VS, Admin, MyUser databaseInitialzer update-database, , DB .

connectionString AttachDbFileName databaseInitialzer, update-database MyUser DB App_Data, MyUser DB . MyUser, , . - LocalDb.

- ?

+4
5

DB, SqlLocalDB. LocalDb can not ( DB) , . JSobells CodingwithSpikes :

ef5-cannot-attach-the-file-0-as-database-1.

'sqllocaldb.exe stop v11.0 ' sqllocaldb.exe v11.0 PM

UPDATE:

SSMS. : ()\v11.0, Windows. localdb.

+13

VS 2013 EF6.1.

, . . , - SQLExpress .

.

  • (.mdf .ldf).
  • .
  • .
  • Windows (IF VS 2012 , VS).
  • "sqllocaldb.exe stop v11.0" ( v11.0)
  • "sqllocaldb.exe delete v11.0" ( v11.0).
  • Visual Studio.
  • "Enable-Migrations" ( )
  • "Add-Migration init" ( )
  • "Update-Database" ( )
+5

= (LocalDb)\v11.0;

to

Server =\SQLExpress;.

Code First

+1

, , .

: 1 { , }

PM:

  • Enable-Migrations -ContextTypeName ContosoUniversity.DAL.SchoolContext

  • add-migration

  • ,

0

, , EF6 MVC5.

The problem occurs due to incorrect initialization of the database in this example.

Edit

public class SchoolInitializer : System.Data.Entity. DropCreateDatabaseIfModelChanges<SchoolContext>

to

public class SchoolInitializer : CreateDatabaseIfNotExists<SchoolContext>

and he will create the database without any problems.

The initialization method used in the example does not create a new database by default. Not sure if this is a mistake or not.

0
source

Source: https://habr.com/ru/post/1525532/


All Articles