简体   繁体   English

实体框架 - 只有没有模板的 edmx

[英]Entity Framework - only edmx without templates

We would like to add only .EDMX files without corresponding .tt files for context and entities.我们只想添加 .EDMX 文件,而不为上下文和实体添加相应的 .tt 文件。 There is multiple reasons for this, but long story short - we would like to be able to have .EDMX and be able to refresh it from DB when needed.这有多种原因,但长话短说 - 我们希望能够拥有 .EDMX 并能够在需要时从 DB 刷新它。

Currently if I delete .tt files every time I "Update from DB" it will regenerate .tt files and entitites which we don't need.目前,如果我每次“从数据库更新”时删除 .tt 文件,它将重新生成我们不需要的 .tt 文件和实体。

If it's enough to have only the .edmx with two empty .tt files you can do that:如果只有带有两个空.tt文件的.edmx就足够了,您可以这样做:

  1. Add the Entity Data Model (EF Designer from database)添加实体数据模型(来自数据库的 EF 设计器)
  2. Cancel initial execution of the tools取消工具的初始执行
  3. Empty Custom Tool value on each .tt file and on the .edmx file每个.tt文件和.edmx文件上的空Custom Tool
  4. Delete contents of the .tt files if you want如果需要,请删除.tt文件的内容
  5. Delete .diagram file删除.diagram文件

After this steps, you can update your .edmx file and nothing happens.完成这些步骤后,您可以更新您的.edmx文件,并且没有任何反应。

自定义工具为空 最终解决方案

I'm not sure I understand fully, but I simply delete the pair of t4/tt files that are included with the model and the previously generated class files.我不确定我是否完全理解,但我只是删除了模型中包含的一对 t4/tt 文件和之前生成的类文件。 They don't come back for EF6.他们不会回来参加 EF6。

在此处输入图片说明

Then I delete them and I update model from database:然后我删除它们并从数据库更新模型:

在此处输入图片说明

and they don't come back:他们不会回来:

他们不会回来

This is from EF 6.2:这是来自 EF 6.2: 在此处输入图片说明

Are you sure you're not dealing with some source control issue?你确定你没有处理一些源代码控制问题吗?

You can try this.你可以试试这个。

  1. Delete .tt files under your .edmx file.删除.tt在你的文件.edmx文件。
  2. Project -> Add -> New Item -> EF 6.x DbContext Generator项目 -> 添加 -> 新项目 -> EF 6.x DbContext Generator
  3. It will generate new .tt files.它将生成新的.tt文件。 In these .tt files you can specify .edmx file path.在这些.tt文件中,您可以指定.edmx文件路径。 在此处输入图片说明

  4. Run these .tt files to generate Model1 classes.运行这些.tt文件以生成Model1类。

  5. Disable .tt files (Don't delete content as you will need it if you want to update your entities later. Usually they generate new files when you save them)禁用.tt文件(不要删除内容,因为如果您想稍后更新您的实体,您将需要它。通常它们会在您保存时生成新文件) 在此处输入图片说明

  6. Update your .edmx (add/remove tables).更新您的.edmx (添加/删除表格)。

  7. This will not generate new .tt files under edmx这不会在 edmx 下生成新的.tt文件
  8. And it will not update your entity classes.它不会更新您的实体类。
  9. And if you want to update your entity classes, then run .tt files.如果您想更新实体类,请运行.tt文件。

在此处输入图片说明

I tried this and "Update Model From Database" didn't generate new .tt files.我试过了,“从数据库更新模型”没有生成新的 .tt 文件。 So hopefully your problem will be solved.所以希望你的问题会得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM