EF6代码第一:启动时如何从EDMX文件加载DbCompiledModel? [英] EF6 code first: How to load DbCompiledModel from EDMX file on startup?

查看:540
本文介绍了EF6代码第一:启动时如何从EDMX文件加载DbCompiledModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将DbCompiledModel缓存到磁盘来减少EF6中的启动时间。



很容易为DbContext写入EDMX文件:

  EdmxWriter.WriteEdmx(myDbContext,XmlWriter.Create(@C:\temp\blah.xml))

很容易将DbCompiledModel传递给DbContext:

  var db = new DbContext(connectionString,myDbCompiledModel)

然而, t似乎有任何方式将EDMX文件从磁盘读取到DbCompiledModel中!注意,我已经使用EF6分支版本中的EdmxReader工具成功实现了解决方案:

  https://github.com/davidroth/entityframework/tree/DbModelStore 

然而,我不愿在生产环境中使用分支版本。我已经尝试从该分支中​​提取EdmxReader实用程序,但它依赖于我无法访问的DbCompiledModel的内部构造函数。



那么,如何获取EDMX文件从磁盘转换成DbCompiledModel?

解决方案

我测试了我是否可以通过序列化DbCompiledModel来使其工作。 / p>

从EF中获得它们,并在构建新的上下文时提供它。问题是一切都是私人的,所以它不会序列化任何东西。



如果你可以得到序列化程序,你可以使用序列化私人成员,它应该是非常简单的。



1)在OnModelCreating结束(如果您使用的是第一个代码),您可以执行

 code 

$ b $ p $ <因为你应该提供一些参数



2)序列化那个。要与私人会员一起尝试查看 JSON.Net:强制所有私有字段和子类中所有字段的序列化或尝试使用BinaryFormatter 为什么BinaryFormatter将私有成员序列化而不是XMLSerializer或SoapFormatter?



3)将其保存到磁盘



4)从磁盘读取文件并将其反序列化为新的DbCompiledModel


I want to reduce startup time in EF6 by caching the DbCompiledModel to disk.

It's easy to write the EDMX file for a DbContext:

EdmxWriter.WriteEdmx(myDbContext, XmlWriter.Create(@"C:\temp\blah.xml"))

And it's easy to pass a DbCompiledModel to the DbContext:

var db = new DbContext(connectionString, myDbCompiledModel)

However there doesn't seem to be any way to read the EDMX file from disk into a DbCompiledModel! How can I do this?

NOTE that I have successfully implemented the solution using the EdmxReader tool in this branched version of EF6:

https://github.com/davidroth/entityframework/tree/DbModelStore

However I am reluctant to use a branch version in a production environment. I have tried extracting the EdmxReader utility from this branch, but it relies on an internal constructor of DbCompiledModel which I can't access.

So, how can I get the EDMX file from disk and convert it into a DbCompiledModel?

解决方案

I tested if I could get it to work by serializing the DbCompiledModel.

Both getting it from EF and providing it when building a new context works. The problem is that everything is private so it will not serialize anything.

If you can get the serializer you use to serialize private members it should be quite simple.

1) In the end of OnModelCreating (if you are using code first) you can do

modelBuilder.Build().Compile()

Slightly simplified as you should provide some arguments

2) Serialize that one. For work with private members try looking at JSON.Net: Force serialization of all private fields and all fields in sub-classes or try to use the BinaryFormatter Why is the BinaryFormatter serializing private members and not the XMLSerializer or the SoapFormatter ?

3) Save that to disk

4) Read the file from disk and Deserialize it to a new DbCompiledModel

这篇关于EF6代码第一:启动时如何从EDMX文件加载DbCompiledModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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