简体   繁体   中英

Two identical generated databases with Entity Framework 6

I have build an application with Entity Framework 6, created some methods to insert an extract data from a database, and I would now like to test it for both a production- and a debugging environment.

To be sure it all works as I would like it to, the debugging database should erase all data for my tests, while my production should keep its data.

I have two projects: MyApp.Database and MyApp.Database.Test , and they each have a connection-string in their app.config -file, which the program loads as so:

public DatabaseContext() : base("name=MyDB")
{
    System.Data.Entity.Database.SetInitializer<DatabaseContext>(new CreateDatabaseIfNotExists<DatabaseContext>()); 
}

and connection-string, where the database parameter set to MyProdDB and MyTestDB :

<connectionStrings>
    <add name="MyDB" connectionString="Host=localhost;user id=myUser;password=myPassword;database=MyProdDB" providerName="Npgsql" />
</connectionStrings>

When I run the application, and run the tests, I get the correct connection-string for each type of database. But I get an error when I run my tests : 42P01: relation "dbo.Tags" does not exist . A simple message, saying I have not migrated my data into my test database.

But how do I migrate it into the test database?

I tried selecting the test-project in the Package manager Console , and running the following commands:

PM> Add-Migration "Init"
No migrations configuration type was found in the assembly 'MyApp.Database.Test'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No migrations configuration type was found in the assembly 'MyApp.Database.Test'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

PM> Enable-Migrations
No context type was found in the assembly 'MyApp.Database.Test'.

Do I need to specify my own DbContext class for MyApp.Database.Test which is almost a replica of MyApp.Database ?

If you don't need data in your DEV environment, just drop database. You use CreateDatabaseIfNotExists, so it will create new database with up-to-date schema. More then it, you can configure your initializer in config file. Use CreateDatabaseIfNotExists inializer on PROD and DropCreateDatabaseIfModelChanges on DEV. So if model will change, you will have to use migrations on PROD, but DEV will just drop database and create again.

Example:

<contexts>      
  <context type="Elmah.SqlServer.EFInitializer.ElmahContext, Elmah.SqlServer.EFInitializer">
    <databaseInitializer type="Elmah.SqlServer.EFInitializer.ElmahDatabaseInitializer, Elmah.SqlServer.EFInitializer" />
  </context>      
</contexts>

or look here

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