Entity Framework 4.1 DbContext 生成器问题
我是 Entity Framework 4.1 的新手,我真的很想为我的模型过渡到 POCO 类。我发现使用安装 EF 4.1 时提供的“DbContext Generator”项非常容易。它完全按照我的意愿进行操作,并为我现有的 EDMX 模型生成了 DbContext 对象和所有 POCO。
我运行了该应用程序并测试它仍然可以工作。是的。我对此很满意,删除了 EDMX 文件和 T4 模板,并开始重新组织我的新 POCO。然而,在让它再次正确构建后,我遇到了运行时问题。实例化 DbContext 时,无法找到元数据文件:.csdl、.ssdl、& .msl(我真的不知道它们是什么,只是它们是所有 EF 连接字符串的一部分)。
放回我的 EDMX 后,它再次运行良好。我真的不再需要 EDMX 文件了。我真的很想坚持使用 POCO 类,而忘记 EDMX 曾经存在过;特别是因为我不希望它运行那些 T4 模板并重新生成我的 POCO。
我有四个相关问题:
- 为什么我必须将 edmx 保留在我的项目中?
- 有解决方法吗?
- 当您使用 EF 4.1 执行真正的“代码优先”时会发生什么,它从哪里获取这些元数据文件?
- 这些元数据文件到底是什么?
I am new to Entity Framework 4.1 and I really wanted to transition to POCO classes for my model. I found that this was very easy using the "DbContext Generator" item provided when you install EF 4.1. It did exactly what I wanted it to do and generated the DbContext object and all the POCOs for my existing EDMX model.
I ran the app and tested that it was still working. It was. Happy with this I deleted the EDMX file and the T4 templates and began reorganizing my new POCOs. However, after getting it to build properly again I encountered a runtime problem. When instantiating the DbContext it is unable to find the metadata files: .csdl, .ssdl, & .msl (I really don't know what they are, just that they are part of all EF connection strings).
After putting back my EDMX it ran fine again. I really don't want the EDMX file anymore. I would really like to stick to POCO classes and forget that the EDMX ever existed; especially because I don't want it running those T4 templates and regenerating my POCOs.
I have four related questions:
- Why do I have to keep the edmx in my project?
- Is there a workaround for this?
- What happens when you do true "code-first" with EF 4.1, where does it get those metadata files from?
- What are those metadata files anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该阅读本文。它描述了不同的项目类型。
简而言之,您在使用 dbcontext 生成器时需要 .edmx 文件。如果您想使用代码优先,则必须手动描述模型或使用实体框架强大工具 CTP1 对代码优先模型进行逆向工程。
You should read this. It describes the different project types.
The short answer is you need the .edmx file when using the dbcontext generator. If you want to use Code First, you have to either describe the model by hand or use the Entity Framework power tools CTP1 to reverse engineer a code first model.
EDMX 文件负责嵌入 CSDL、SSDL 和编译时程序集中的 MSL 元数据资源。
是的。您将需要 Entity Framework Power Tools CTP 1。请参阅 Mystere Man 对此问题的回答以获取链接。这将是从现有数据库到代码优先的真正转换。这意味着您的代码将推动架构向前发展,而不是相反。
当您进行真正的代码优先开发时,CSDL、SSDL 和MSL 元数据资源仍然存在。它们只是在构建时从对象模型中推断出来的。由于代码控制模型和架构,因此它能够自行推断这些资源。但在某些情况下,它不知道从代码中推断出什么。例如,如果您更改实体属性名称,它无法知道您是否打算删除旧列(属性名称)并添加具有新名称的新列,或者您是否打算保留旧列并重命名它与新名称。这就是为什么 EF 代码优先无法实现良好的数据/架构迁移,以及每次在代码中更改模型时它都会删除并重新创建整个数据库的原因。
DbContext API 的这个缺点绝对是最大的问题,目前 ADO.NET 团队正在解决该问题,如 斯科特·汉斯勒曼的博客文章。
目前,我个人选择手动修改数据库,然后简单地更新我的 C# 代码来反映它。这需要对这些东西如何工作有一定的了解,我仍在练习。另一种方法是将 EDMX 文件保留在那里,像平常一样更新它,并在情况发生变化时让它重新生成 POCO。然而,这与经典 EF 更新几乎没有什么不同,因为对 POCO 的任何更改在重新生成时都将被覆盖。
另一个解决方案是再次将 EDMX 文件保留在那里,并在情况发生变化时更新它,但将“保存时转换相关文本模板”属性设置为 false。
http://www.codetunnel.com/content/images/TransformRelatedTextTemplatesOnSave.jpg
这会阻止 EDMX 运行您的 T4 模板并重新生成 POCO。因此,您只需更新它即可确保元数据资源保持准确。但随后您将手动修改 POCO 以反映模型/架构的任何更改。这仍然需要做更多的工作,而不仅仅是让它生成您的代码,但以我的拙见,这对学习很有好处。
CSDL、SSDL 和MSL 是 EF 的元数据资源。它们帮助 EF 确定如何将结果集映射到对象模型,反之亦然。它是数据库模式和代码之间的桥梁。 这些资源都存在,只是生成方式不同。
The EDMX file is responsible for embedding the CSDL, SSDL, & MSL metadata resources in the assembly at compile-time.
Yes. You will need Entity Framework Power Tools CTP 1. See Mystere Man's answer on this question for a link. This will be a true conversion from an existing database to code-first. What this means is that your code will drive the schema going forward, not the other way around.
When you are doing true code-first development the CSDL, SSDL, & MSL metadata resources are still there. They are simply inferred from your object model when you build. Because the code controls the model and schema, it is able to infer these resources on its own. There are some instances though where it does not know what to infer from the code. For instance, if you change an entity property name it has no way of knowing if you intended to delete the old column (property name) and add a new column with the new name, or if you intended to keep the old column and rename it with the new name. This is why there is no good data/schema migration with EF code-first and why it drops and recreates the entire database each time you change your model in code.
This shortcoming of the DbContext API is definitely the biggest issue and is currently being worked on by the ADO.NET team as noted on Scott Hansleman's blog post.
For now I have personally opted to modify the database manually and then simply update my C# code to reflect it. This requires some decent knowledge of how these things work and I'm still practicing. Another way to do this is to leave the EDMX file there, update it like you normally would, and let it regenerate your POCOs when things change. However that is almost no different than doing classic EF updates as any changes to the POCOs will be overwritten when they are regenerated.
Yet another solution is to again leave the EDMX file there and update it when things change, but set the "Transform Related Text Templates On Save" property to false.
http://www.codetunnel.com/content/images/TransformRelatedTextTemplatesOnSave.jpg
This prevents the EDMX from running your T4 templates and regenerating your POCOs. So you would simply update it to ensure that the metadata resources remain accurate. But then you would manually modify your POCOs to reflect any changes to the model/schema. This still requires a lot more work than just letting it generate your code but it's good for learning in my humble opinion.
The CSDL, SSDL, & MSL are metadata resources for EF. They help EF determine how to map result sets to your object model and vice versa. It is the bridge between the database schema and your code. No matter what way you use EF these resources are there, only differing in the way they are generated.
这样不行。如果您决定使用 DbContext Generator,则表示您的映射将在 EDMX 文件中描述。在编译过程中,EDMX 文件被分解为三个独立的文件,扩展名为 .ssdl、.csdl 和 .msl。这些元数据文件描述您的数据库、实体以及从数据库到实体的映射。默认情况下,这些文件作为资源存储在已编译的程序集中,并从传递到
DbContext
实例的连接字符串中引用。如果您想使用生成器创建的DbContext
,则必须将 EDMX 文件保留在项目中。它与 EFv1 和 EFv4 中使用的映射方法相同 = 它不是代码优先。代码优先意味着没有 EDMX 和 T4 模板来为您生成映射和代码。通常,代码优先意味着您在拥有任何数据库之前编写类,并且数据库是由 EF 创建的。许多人在现有数据库中使用这种方法,并手动将它们的类映射到现有数据库,但这需要一些 EF 知识/经验(或者您可以在此处提出有针对性的问题,我们将为您提供帮助)。
上下文需要元数据来执行映射和 SQL 命令生成。 EDMX 映射策略使用上述 XML 文件中定义的元数据。代码优先映射策略使用一些默认约定、Fluent-API 或数据注释来从编译的代码中获取元数据。
有一个单独的项目允许从现有数据库生成代码优先映射 - 我在您的 上一个问题。
That doesn't work this way. If you decide to use DbContext Generator you are saying that your mapping will be described in EDMX file. During compilation EDMX file is decomposed to three separate files with .ssdl, .csdl and .msl extensions. These metadata files describes your database, entities and mapping from database to entities. These files are by default stored as resources in your compiled assembly and referenced from connection string passed to your
DbContext
instance. You must keep your EDMX file in the project if you want to useDbContext
created by generator. It is same mapping approach as was used in EFv1 and EFv4 = it is not code first.Code first means no EDMX and no T4 template to generate mapping and code for you. In normal way code first means that you write your classes before you have any database and a database is created by EF. Many people are using this approach with existing database and mapping they classes to existing database manually but it requires some knowledge / experience with EF (or you can ask targeted questions here and we will help you).
Metadata are needed for context to perform mapping and SQL command generation. EDMX mapping strategy uses metadata defined in mentioned XML files. Code first mapping strategy uses some default conventions, fluent-API or data annotations to get metadata from compiled code.
There is separate project allowing to generate code first mapping from existing database - I pointed that project in your previous question.