简体   繁体   English

实体框架C#代码优先迁移单元测试

[英]Entity Framework C# Code First Migration Unit Testing


I would like to be able to unit test using my code first/migrations entity framework project. 我希望能够使用我的代码优先/迁移实体框架项目进行单元测试。

My current method does the following: 我当前的方法执行以下操作:

  1. Create the DataContext 创建DataContext
  2. Creates the repository 创建存储库
  3. Creares unit of working using DataContext 使用DataContext的Creares工作单元
  4. Creates my service layer 创建我的服务层

After step 1. I would like to be able say to entity framework, using my migrations/migrations folder recreate the database every time it runs. 在步骤1之后。我想对实体框架说一下,使用我的migrations / migrations文件夹在数据库每次运行时都重新创建它。

I current send in my Datacontext a custom string which represents what connection string to use, my code looks like: 我当前在Datacontext中发送一个自定义字符串,该字符串代表要使用的连接字符串,我的代码如下所示:

    [TestCase]
    public void TestMethod1()
    {

        var systemContextTest = new SystemContext("SystemContextTest");
        IRepository<PublicQueries> repos = new Repository<PublicQueries>(systemContextTest);
        var uow = new UnitOfWork(systemContextTest);
        var service = new PublicQueryService(repos);
        service.Find(0);
    }

I just want to be able to create the database, using migrations everytime this test is ran! 我只希望能够在每次运行此测试时使用迁移来创建数据库!

Thanks 谢谢

-UPDATE- -更新-

I have tried: 我努力了:

Database.SetInitializer<YourContext>(new MigrateDatabaseToLatestVersion<YourContext, YourMigrationConfiguration>());

But this results in 但这导致

System.Data.Entity.Core.EntityCommandExecutionException : An error occurred while executing the command definition. System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。 ----> System.Data.SqlClient.SqlException : Invalid object name ----> System.Data.SqlClient.SqlException:无效的对象名称

Which means its still not creating it. 这意味着它仍然没有创建它。

I just want to using my migration files/code first migrations to create a new fresh database. 我只想使用迁移文件/代码的首次迁移来创建新的新数据库。

-- UPDATE 2 -- -更新2-

When adding Database.SetInitializer(new DropCreateDatabaseAlways<SystemContext>()); 添加Database.SetInitializer(new DropCreateDatabaseAlways<SystemContext>());

I get the following error: 我收到以下错误:

System.InvalidOperationException : The DropCreateDatabaseAlways initializer did not drop or create the database backing context 'SystemContext' because Migrations are enabled for the context. System.InvalidOperationException:DropCreateDatabaseAlways初始化程序未删除或创建数据库支持上下文“ SystemContext”,因为已为上下文启用了迁移。 Use Migrations to manage the database for this context, for example by running the 'Update-Database' command from the Package Manager Console. 使用迁移来为此上下文管理数据库,例如,通过从程序包管理器控制台中运行“ Update-Database”命令。

My SystemContext Constructor i am using for testing looks like: 我用于测试的SystemContext构造函数如下所示:

      public SystemContext(string connectionstringname) :
        base(connectionstringname)
    {
        Database.SetInitializer<SystemContext>(null);
        Configuration.ProxyCreationEnabled = false;
    }

Updatad answer: 更新的答案:

Implement a custom database initializer 实现自定义数据库初始化程序

private DbMigrationsConfiguration configuration;

public void InitializeDatabase(C context) {
    context.Database.Delete();
    context.Database.Create();

    var migrator = new DbMigrator(configuration);
    migrator.Update();
}

and put this line at the beginning of the test method. 并将这一行放在测试方法的开头。

Database.SetInitializer(new MyCustomInitializer<SystemContext>());

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

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