EF Code-first:如何缓存DbCompiledModel?
我使用代码优先方法为 EF 4.1 RC 设置了一个简单的测试项目。我看到,每次调用 new MyContext() 都需要相当长的时间。 我发现,有一个接受 DbCompiledModel 和描述的构造函数 http://msdn.microsoft.com/en-us/library/system.data.entity.infrastruct.dbcompiledmodel(v=VS.103).aspx 说,最好缓存这个对象,以获得更好的性能。 但我找不到如何从现有上下文中获取 DbCompiledModel。而且网上也没有样品。
I have setup a simple test project for the EF 4.1 RC, using Code-First aproach. What I see, that every call to new MyContext() is taking quite a long time.
I found, that there is a constructor which accept DbCompiledModel and the description http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcompiledmodel(v=VS.103).aspx says, that it is good to cache this object, for better performance.
But I can`t find how to get the DbCompiledModel from existing context. And there are no samples on the net.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模型已被缓存。您可以通过将断点放置到
OnModelCreating
方法中来轻松验证它。仅当您第一次创建上下文时才会命中它。如果您想手动创建DbCompiledModel
,则必须首先在上下文之外创建DbModelBuilder
。使用其Build
方法,然后Compile
生成DbModel
。Model is already cached. You can validate it easily by placing breakpoing to your
OnModelCreating
method. It will be hit only first time you create context. If you want to createDbCompiledModel
manually you must first createDbModelBuilder
outside of your context. Use itsBuild
method and thenCompile
resultingDbModel
.这是我重新思考后发现的。您可以使用以下代码获取已编译模型的副本。
但是,当我将该模型传递到我的上下文并进行一些性能测试,并将它们与对 new MyContext() 的简单调用进行比较时。我发现,该上下文已经缓存了编译后的模型,因为时间是相同的。因此,编译模型的缓存似乎只在手工制作时才需要。
Here`s what I found after rethinking. You can get yourself a copy of compiled model with the following code.
But, when I passed that model to my context and made some performance tests, and compared them with a plain call to new MyContext(). I found, that context is already caching the compiled model, since the times were identical. So, caching of compiled model, seems to only needed, when it is handcrafted.