我需要更改表架构而不重新加载应用程序域(EF 模型缓存问题)
我有一个多租户代码优先系统的自定义实现,基本上是租户的 SQL 架构部门。我使用 ToTable 方法在第一次调用时正确映射架构,但正如我所读到的有关正在缓存的模型的信息,在第二次调用时更改架构时,使用不同的租户是行不通的。 EF 4.1 中是否有任何方法可以禁用缓存或每次都重建模型。是的,我知道这对性能不利。感谢您的帮助..
I have a custom implementation of a multi tenant code first system basically SQL Schema Divisions of the tenants. I am using the ToTable method to map the schema correctly on the first call, but as I have read about the model being cached changing the schema on the second call do a different tenant does not work. Is there any ways in EF 4.1 to disable the caching or to rebuild the model every time.. Yes i know this is not great for performance. Thanks for any help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然这是一个老问题,但对于所有面临这个问题并最终找到这个问题的可能解决方案的人来说。事情是这样的...
最初可以通过在 OnModelCreating 方法中将 ModelBuilder 的“CacheForContextType”属性设置为“false”来关闭缓存。该方法在 DBContext 中定义为虚拟方法,需要重写。但在 EF 4.1 中,此属性已被删除,因为模型创建是一个昂贵的过程,并且 Microsoft 团队希望推广更好的模式。 检查此链接
Although it is an old question, but for all those who face this issue and end up finding this question for a possible solution. Here it goes...
Initially caching could be turned off by setting the "CacheForContextType" property of the ModelBuilder to ‘false’ in the OnModelCreating method. This method is defined in DBContext as virtual and needs to be overridden. But in EF 4.1 this property has been removed, since model creation is an expensive process and the Microsoft team wanted to promote a better pattern. Check this link
看起来模型构建器上的 Build() 命令就是您正在寻找的。
It seems like the Build() command on the ModelBuilder is what you're looking for.