Entity Framework 4.1 RC POCO - 对生成的字段名称进行单元测试
是否可以使用 EF4.1 访问有关 POCO 实体的元数据,其中说明数据库字段名称是什么?最重要的是外键值。
我想访问它以进行单元测试,同时尝试将 EF4.1 应用于现有数据库模式。我无法更改现有的列名称。
目前,测试使用 SQL Server Compact 4.0 创建数据库,因此我可以看到字段的名称,但如果这是自动化的,那就更好了。
测试的伪代码
- 创建实体
- 保存实体
- 检查字段名称是否符合预期
Is it possible to access metadata about a POCO entity using EF4.1 that states what the database field names are? Most importantly on foreign key values.
I would like to access this for the purpose of unit testing whilst trying to apply EF4.1 to an existing database schema. I cannot change the existing column names.
Currently the tests create the database using SQL Server Compact 4.0 so I can see the names of the fields, but it would be better if this was automated.
Pseudo code for a test being
- Create entity
- Save entity
- Check that the field name is as expected
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那不是单元测试。那就是集成测试。而且这样的测试并没有太大用处。不要让 EF 创建数据库,而是使用现有数据库的副本。只需保存实体并在新上下文中加载实体即可。如果数据没问题,你会很满意。您可以将每个测试设为事务性的,以便在每次测试后回滚事务。
采用代码优先方法的 EF 4.1 不提供对元数据工作区的访问。您可以尝试将
DbContext
转换回ObjectContext
并尝试访问MetadataWorkspace
:在
MetadataWorkspace
中搜索任何内容都是糟糕的体验。该类绝对不是为在自定义代码中使用而设计的。That is not unit testing. That is integration testing. Moreover such tests are not too much usefull. Don't let EF create database and instead use copy of the existing database. Simply save entity and load entity in a new context. If data are ok you are happy with it. You can make each test transactional so the transaction is rolled back after each test.
EF 4.1 with code-first approach doesn't offer access to metadata workspace. You can try to convert
DbContext
back toObjectContext
and try to accessMetadataWorkspace
:Searching anything in
MetadataWorkspace
is terrible experience. That class is definitely not designed to be used in custom code.