模拟实体框架数据库
我正在使用实体框架 4+。
是否可以使用一些随机数据从模式自动创建一些 MOCK 数据库?在哪里放置整数,在哪里放置整数,在哪里放置字符串,放置一些 GUID 或其他什么......
这将帮助我调试我的应用程序很多。
谢谢你,詹姆斯
I am using Entity Framework 4+.
Is it possible to automatically create some MOCK database from the schema with some random data? Where ints, put ints, where strings, put some GUIDs or whatever...
It would help me to debug my application a lot.
Thank you, James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现这个很棒的工具可以与 EF 4 一起使用进行模拟数据库。基本上,它只是一个 T4 模板,它创建“普通”EF 层,还创建可用于测试的模拟对象和接口。更多文档此处 。
不过,有一点需要注意。开箱即用的它对我不起作用,因为我的存储库代码调用 SaveChanges() 和其他未在生成的接口中实现的方法。我能够通过从 Microsoft 的 ObjectContext 实现中提取接口并创建我自己的 IObjectContext 接口来解决这个问题。然后,我创建了一个基类(用于模拟),通过简单地将每个调用委托给注入的模拟来实现此接口。这样我就可以使用 Moq 在我的测试类中创建该部分,同时仍然保留在生成的模拟中跟踪插入、更新和删除的能力。
以下是我对“Context”T4 模板的更改,以解决此问题。抱歉,我是零碎地做这件事 - 所以没有让我发布整个代码清单,因为它太长了。
在
fileManager.Process();
之前添加此代码在
WriteInterface()
块之后添加此代码。将
WriteMockContextBody()
(注释后)的前 2 行(第二行是大括号)更改为Change the 第一行
WriteInterface()
(注释后) )我还应该注意,我还没有对此进行太多测试,但我确实做了一些基本验证以确保其功能。
I discovered this great tool for use with EF 4 for mocking the database. Basically it is just a T4 template that creates both the "normal" EF layer and also creates a mock object and interface that can be used to test. More documentation here.
One caveat, though. Out of the box it didn't work for me because my repository code calls SaveChanges() and other methods which are not implemented in the generated interface. I was able to get around this by extracting the interface from Microsoft's ObjectContext implementation and creating my own IObjectContext interface. I then created a base class (for the mock) that implements this interface by simply delegating each of the calls to an injected mock. This way I can use Moq to create that piece in my test class, while still retaining the ability to track inserts, updates, and deletes in the generated mock.
Here are my changes to the "Context" T4 template to fix this issue. Sorry for doing this in little bits and pieces - SO didn't let me post the entire code listing because it was too long.
Add this code before
fileManager.Process();
Add this code after the
WriteInterface()
block.Change the first 2 lines (the 2nd one being the curly bracket) of
WriteMockContextBody()
(after the comment) toChange the first line of
WriteInterface()
(after the comment) toI should also note that I haven't done much testing with this yet, but I did do some basic verifications to ensure it functions.
这是帮助生成测试数据的库 - http://autopoco.codeplex.com/
Here is library that helps in generating test data - http://autopoco.codeplex.com/
请参阅 Rab Hallett 的博客,标题为 ADO.NET 模拟上下文生成器:视觉Studio 2010 模板
这里 Rab 使用 T4 模板来创建 EF 的界面。
See Rab Hallett's blog titled ADO.NET Mocking Context Generator: Visual Studio 2010 Template
Here Rab uses a T4 template to create the interface for EF.
您可以查看此线程 关于面向数据库专业人员的 Visual Studio。或者 RedGate 有一个类似的工具。我不知道有什么免费工具可以做到这一点。
You can check out this thread about Visual Studio for Database Professionals. Or RedGate has a similar tool. I don't know of any free tools that would do this.