EntityTypeConfiguration-测试针对数据库的映射的干净方法是什么? [英] EntityTypeConfiguration - What is a clean method to test mapping against the database?

查看:71
本文介绍了EntityTypeConfiguration-测试针对数据库的映射的干净方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我公司的当前结构是使用Plinqo/Linq to Sql创建数据访问对象",然后使用一组自定义的CodeSmith模板来构建业务对象".简而言之,这两组对象之间的联系非常紧密,使用Linq to SQL,会导致非常丑陋的解决方法.

My company's current structure is using Plinqo/Linq to Sql to create "data access objects", and then use a custom set of CodeSmith templates to build "business objects". To make a very long story short, these two sets of objects are very tightly coupled and, with Linq to SQL, lead to pretty ugly workarounds.

在生成dbml之后,Plinqo模板将表直接按1:1映射到类.这带来了一些安慰,因为如果数据库发生更改,那么业务对象方面(或应用程序方面)会出现编译时错误.

The Plinqo templates do a direct 1:1 mapping of table to class after generating the dbml. This leads to some comfort in that if the database changes, there is a compile-time error on the business object side (or application side).

我正在慢慢地尝试证明EF 4.1(代码优先)映射到现有架构的好处,但是这种代码生成的类型安全性"已成为关键利益相关者心目中的一个大问题.

I am slowly trying to prove out the benefits of EF 4.1 (Code First) to map to the existing schema, but this "type safety" of code generation has come up as a big issue in a key stakeholder's mind.

问题:

因此在实体框架4.1中,我首先使用代码来映射到现有数据库.

So in entity framework 4.1, I am using code first to map to the existing database.

  • Poco域对象
  • 每个映射的
  • EntityTypeConfiguration

作为测试项目,您有何建议以确保到架构的映射是正确的?我应该只创建一个单元测试项目并检索每个对象还是有更好的方法?

What would you suggest as a test project for ensuring that the mapping to the schema is sound? Should I just create a unit test project and do retrievals of each object or is there a better way?

谢谢!

推荐答案

我使用了一个基础通用集成测试来执行CRUD操作,而派生测试仅包含用于创建实体和验证每个操作结果的方法.每个测试方法都在未提交的事务范围内运行,因此测试数据库仍处于初始状态.

I used one base generic integration test performing CRUD operations and derived tests only contained methods for creating entity and validating results of each operation. Each test method run in transaction scope which didn't commit so the test database was still in initial state.

在开始使用存储库而不是使用单个实体类型的情况下,您将开始使用聚合根的情况可以进一步改善.在这种情况下,创建操作聚合根的正确集成测试非常方便.

This can be further improved in scenarios where you start to use repositories and instead of working with single entity types you will start to work with aggregate roots. In such case creating correct integration tests manipulating aggregation roots is very handy.

这篇关于EntityTypeConfiguration-测试针对数据库的映射的干净方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆