c# - 测试时的努力问题

标签 c# .net entity-framework unit-testing

我一直在阅读有关使用 Effort 进行单元测试的文章,因为在内存中设置数据库非常“容易”。我一直在尝试在我的测试数据服务中实现这一点,但我一直遇到问题。 我正在使用 Entity Framework 6 和从现有表生成的模型。

上下文模型

public partial class Entities : DbContext
{
    public Entities()
        : base("name=Entities")
    {
    }

    //Overloading for effort unit test
    public Entities(DbConnection connection) : base(connection,true)
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<REPAIR_CHECK_IN_TABLE> REPAIR_CHECK_IN_TABLEs { get; set; }
    public virtual DbSet<tblPlanarWarranty> tblPlanarWarranties { get; set; }
}

测试数据服务

private IDataLoader loader;
    private Entities context;


    public TestDataService()
    {

        loader = new Effort.DataLoaders.CsvDataLoader(@"_TestDataFiles\");
        context = new Entities(Effort.DbConnectionFactory.CreateTransient(loader));
    }

最初我会收到以下错误。

No entity Framework provider for the ADO.NET provider withe invariant name 'Effort.Provider'

然后我将所述提供程序添加到应用配置文件的我的提供程序部分并开始收到此错误。

The 'Instance' member of the entity Framework provider type 'Effort.ProviderServices,Effort,Version-1.0.0.0 didt not return an object that inherits from 'System.Data.Entity.Core.Common.DBProviderServices'

有没有人知道我做错了什么?在网站上,它就像几行一样简单,但这已经变成了一个巨大的困惑。

最佳答案

小心!使用 EF6 时,不要安装 Effort by ZZZ Projects!

通过相同的 ZZZ 项目安装 Effort.EF6

class SchoolDbContext : DbContext
{
    // the standard constructors you probably already use:
    public SchooldDbContext() : base(...) {}
    public SchoolDbContext(string nameOrConnectionString) : base(nameOrConnectionString) {}

    // add this one for effort:
    public SchoolDbContext(DbConnection existingConnection, bool contextOwnsConnection) 
        : base(existingConnection, contextOwnsConnection) { }

    ... // DbSets, etc

使用干净的空数据库

var dbConnection = Effort.DbConnectionFactory.CreateTransient();
using (var dbContext = new SchoolDbContext(dbConnection, true)
{
      dbContext.Schools.Add(new School {...});
      dbConext.SaveChanges();

      // this database exists as long as the connection exists
      var schoolCount = dbContext.Schools.Count();
      // expect one school
}

// a new empty database will be created: there won't be any schools
dbConnection = Effort.DbConnectionFactory.CreateTransient();
using (var dbContext = new SchoolDbContext(dbConnection)
{
     var schoolCount = dbContext.Schools.Count();
     // expect no schools
}

重用数据库

此数据库将在您的程序运行期间保留。该模型只会创建一次。

const string dbName = "MyDb"
var dbConnection = Effort.DbConnectionFactory.CreatePersistent(dbName);
using (var dbContext = new SchoolDbContext(dbConnection)
{
      dbContext.Schools.Add(new School {...});
      dbContext.SaveChanges();
}

dbConnection = Effort.DbConnectionFactory.CreatePersistent(dbName);
using (var dbContext = new SchoolDbContext(dbConnection)
{
     var schoolCount = dbContext.Schools.Count();
     // expect the school you just added
}

关于c# - 测试时的努力问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38441528/

相关文章:

.net - WPF 中有内置的 IMultiValueConverter 吗?

.net - AWS 中的 SQL Server CLR 存储过程

c# - 从对象设置 ComboBox 的 SelectedItem

c# - Entity Framework - 未映射的实体。可能的?

c# 3D Bin Packing for shipping 运输

c# - WPF 按钮 ControlTemplate 不会覆盖继承的前景

c# - UltraGrid 过滤器单元格属性

entity-framework - EF : Is it OK to use DbSet<T>. AddOrUpdate() 在迁移之外?

c# - Azure 配置设置和 Microsoft.WindowsAzure.CloudConfigurationManager

c# - 本地 UWP 客户端到本地 asp.net core api web 服务器 - 证书颁发机构无效或不正确