繁体   English   中英

使用Entity Framework 6生成两个相同的数据库

[英]Two identical generated databases with Entity Framework 6

我已经使用Entity Framework 6构建了一个应用程序,创建了一些方法来插入数据库中的提取数据,现在我想在生产环境和调试环境中对其进行测试。

为确保一切正常,调试数据库应删除测试的所有数据,而生产环境应保留其数据。

我有两个项目: MyApp.DatabaseMyApp.Database.Test ,他们每个人的app.config -file中都有一个连接字符串,程序将按以下方式加载:

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

和连接字符串,其中database参数设置为MyProdDBMyTestDB

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

当我运行应用程序并运行测试时,对于每种类型的数据库,我都会获得正确的连接字符串。 但是运行测试时出现错误: 42P01: relation "dbo.Tags" does not exist 一条简单的消息,说我还没有将数据迁移到测试数据库中。

但是,如何将其迁移到测试数据库中?

我尝试在Package manager Console选择测试项目,然后运行以下命令:

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'.

我是否需要用户自己指定DbContextMyApp.Database.Test这是几乎翻版MyApp.Database

如果您在DEV环境中不需要数据,只需删除数据库。 您使用CreateDatabaseIfNotExists,因此它将创建具有最新架构的新数据库。 然后,您可以在配置文件中配置初始化程序。 在PROD上使用CreateDatabaseIfNotExists初始化器,在DEV上使用DropCreateDatabaseIfModelChanges。 因此,如果模型将更改,则必须在PROD上使用迁移,但是DEV将删除数据库并再次创建。

例:

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

或看看这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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