实体框架 4 的内存数据库

发布于 2024-10-02 19:01:23 字数 308 浏览 1 评论 0原文

有谁知道与 .NET 4/EF 4 配合良好的内存数据库吗?具体来说,我正在考虑单元测试,这样每个设置都可以轻松创建数据库,并用默认值填充它,并且每次拆卸都可以快速销毁它。

我听说 SQLite 尚不支持 .NET 4,还有其他使用它作为 SQLServer 的替代品(应用程序将在发布模式下运行)时遇到了麻烦。

过去,我使用 DevExpress XPO ORM,它有一个内置的内存数据库,非常适合单元测试。

Does anyone know of a good in-memory DB that works well with .NET 4/EF 4? Specifically, I'm thinking of unit testing, such that each setup can easily create the DB, and populate it with default values, and each teardown can destroy it - in a quick fashion.

I've heard that SQLite doesn't support .NET 4 yet, and others have had trouble using it as a substitute for SQLServer (which is what the appliction will run on in release mode).

In the past, I used DevExpress XPO ORM and it had a built-in in-memory database that worked quite well for unit testing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淤浪 2024-10-09 19:01:23

如果您的模型具有数据库生成的密钥(通常在 EF4 中使用 [DatabaseGenerate(DatabaseGeneeratedOption.Identity)] 注释指定),则使用 CE4 进行单元测试时将会出现问题,因为 CE4 不支持此功能。

但我找到了一个简单的解决方法,无需修改我的生产代码(在 SQL 2008 上运行)。我创建了 DbContext 的代理子类,并在其中重写了 OnModelCreating 以删除注释,并重写了 SaveChanges 以手动设置 ID:

public class TestContext : MyDbContext
{
  protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<MyEntity>().Property(e => e.Id)
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    base.OnModelCreating(modelBuilder);
  }

  public override int SaveChanges()
  {
    foreach (var entry in ChangeTracker.Entries<MyEntity>().Where(e => e.State == EntityState.Added))
    {
      entry.Entity.Id = Guid.NewGuid();
    }
    return base.SaveChanges();
  }
}

通过这些细微的调整,我现在可以在 CE4 中对在生产中的 SQL 2008 下运行的相同存储库代码进行单元测试。

如果您使用整数 ID 而不是 GUID,这里有一个很好的扩展方法,可以直接插入到上述解决方案中: http://enigmadomain.wordpress.com/2010/01/06/sql-compact-identity-columns-and-entity-framework/< /a>

You'll have a problem unit testing with CE4 if your model has database-generated keys (usually specified in EF4 using the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation), as CE4 doesn't support this.

But I found an easy workaround without needing to modify my production code (which runs on SQL 2008). I created a proxy sub-class of my DbContext, and in it overrode OnModelCreating to remove the annotation, and overrode SaveChanges to set the ID manually:

public class TestContext : MyDbContext
{
  protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<MyEntity>().Property(e => e.Id)
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    base.OnModelCreating(modelBuilder);
  }

  public override int SaveChanges()
  {
    foreach (var entry in ChangeTracker.Entries<MyEntity>().Where(e => e.State == EntityState.Added))
    {
      entry.Entity.Id = Guid.NewGuid();
    }
    return base.SaveChanges();
  }
}

With these minor tweaks, I can now unit test the same repository code in CE4 that runs under SQL 2008 in production.

And if you are using integer IDs rather than GUIDs, there's a nice extension method here that can be plugged right into the above solution: http://enigmadomain.wordpress.com/2010/01/06/sql-compact-identity-columns-and-entity-framework/

成熟稳重的好男人 2024-10-09 19:01:23

SQL CE 将为您完成此操作。它是一个在进程中运行的嵌入式数据库。它适用于 EF4(包括 Code First)。获取它的最简单方法是直接安装 NuGet 包。有两个 NuGet 包 - 一个仅具有 SQL CE,另一个具有与 EF Code First CTP4 配合使用的 SQL CE。

SQL CE will do this for you. It's an embedded database that runs in process. It works with EF4 (including Code First). The easiest way to get it is to directly install the NuGet package for it. There are two NuGet packages - one that just has SQL CE and the other that has the SQL CE that works with the EF Code First CTP4.

池予 2024-10-09 19:01:23

如果您切换到 EF CTP4 没有问题,那么就像这样:

EF4 CTP4
SQL CE 4 CTP

当您编写单元测试(代码优先)时,它将在内存中为您生成数据库。

If it's not a problem for you to switch to EF CTP4 then it's like that:

EF4 CTP4
SQL CE 4 CTP

It will generate db in-memory for you as you write unit tests (code-first).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文