简体   繁体   English

如何使用 edmgen.exe 生成 .edmx.diagram 文件?

[英]How to generate .edmx.diagram file using edmgen.exe?

Using the suggestion provided in GitHub , I was able to generate the EDMX files needed for an ASP.net project.使用GitHub 中提供的建议,我能够生成 ASP.net 项目所需的 EDMX 文件。 Using a command like:使用如下命令:

"%windir%\Microsoft.NET\Framework\v4.0.30319\edmgen.exe" /mode:fullgeneration    /c:"Data Source=%datasourceserver%; Initial Catalog=School; Integrated Security=SSPI"    /project:School /entitycontainer:SchoolEntities /namespace:SchoolModel /language:CSharp

But I don't know how to generate the accompanying edmx.diagram file that gets generated in Visual Studio if we create the EDMX via ADO.Net Data Model addition to existing project.但是,如果我们通过 ADO.Net 数据模型添加到现有项目中创建 EDMX,我不知道如何生成在 Visual Studio 中生成的随附edmx.diagram文件。

In the Solution Explorer, the file can be seen in a location like so:在解决方案资源管理器中,可以在如下位置看到该文件: 在此处输入图片说明

The file can also be opened in Visual studio to see the Database structure in the form of a UML diagram like so:该文件也可以在 Visual Studio 中打开,以 UML 图的形式查看数据库结构,如下所示: 在此处输入图片说明

Additionally the file generated for this gets shown like so:此外,为此生成的文件显示如下: 在此处输入图片说明

I read the documentation on how to use edmgen.exe to generate the edmx files as well from the official documentation .我从官方文档中阅读了有关如何使用 edmgen.exe 生成 edmx 文件的文档

I believe that the documentation to generate the edmx.document file has been missed in the Microsoft documentation and I have been unable to come up with a solution for this by myself.我相信 Microsoft 文档中遗漏了生成 edmx.document 文件的文档,我自己也无法为此提出解决方案。 I have been stuck for quite some time on this problem and need help in resolving this.我在这个问题上被困了很长一段时间,需要帮助来解决这个问题。

I have used a similar mechanism to generate files needed in the SQL2LINQ convertor project.我使用了类似的机制来生成SQL2LINQ转换器项目中所需的文件。 Having this capability could help tremendously.拥有这种能力可以提供极大的帮助。 Please help me.请帮帮我。

Edit 1: I have noticed that the edmx.diagram file has property like so.编辑 1:我注意到 edmx.diagram 文件具有这样的属性。 What I am not sure is if Visual Studio internally uses some other executable to generate the diagram files or if there is an un-documented flag that that can create the diagram files via command line.我不确定的是 Visual Studio 是否在内部使用其他一些可执行文件来生成图表文件,或者是否有一个未记录的标志可以通过命令行创建图表文件。 Please forgive my Edit this information was not avaialable while originally posting the question.请原谅我在最初发布问题时无法编辑此信息。

在此处输入图片说明

Edit 2: All the steps involved in the process I use:编辑2:我使用的过程中涉及的所有步骤:

Step1: Copy my resource files to the folder where I require my edmx and dependency files to be generated. Step1:将我的资源文件复制到我需要生成edmx和依赖文件的文件夹中。

Note: These files are dummy files and will be generated from the command line command I have pasted in the question.注意:这些文件是虚拟文件,将从我粘贴在问题中的命令行命令生成。

Step 2: Run the command line command by navigating to the same path.步骤 2:通过导航到相同路径运行命令行命令。

Step 3: After the command line is run, the connection string collected form the user will help in generating the necessary CSDL, SSDL and MSL files in the same directory.步骤3:命令行运行后,从用户收集的连接字符串将有助于在同一目录中生成必要的CSDL、SSDL和MSL文件。 the files are then read and replaced int he edmx files that i have included in the resources folder in the link above.然后这些文件被读取并替换为我在上面链接的资源文件夹中包含的 edmx 文件。

Step 4: Run The textTransform.bat file to run the texttransform.exe from the Windows SDK path for Texttransform.exe.第 4 步:运行 textTransform.bat 文件以从 Texttransform.exe 的 Windows SDK 路径运行 texttransform.exe。

Observation: At this stage, 5 of the 6 files are created namely:观察:在此阶段,创建了 6 个文件中的 5 个,即:

  1. .context.tt .context.tt
  2. .context.cs .context.cs
  3. .Designer.cs .Designer.cs
  4. .tt .tt
  5. .cs .cs

corresponding to the name provided by the user.与用户提供的名称相对应。

But the file .edmx.diagram is missing.但是文件 .edmx.diagram 丢失了。

Code that does step 1 through 4:执行步骤 1 到 4 的代码:

internal class Globals {
    public static string EDMXworkingDirectory = @"C:\ERachana\EDMX\EDMXFiles\EDMXParts";
    public static bool isEDMXAlreadyGenerated = false;

    public static string Server = "",Database = "", UserName = "",Password = "";
    public static string ProjectName = "", UserDefinedObjectName = "_appdb", TemporaryDirectoryPath="";

    public static string GetSubstringBetweenStrings(string Full, string startMatch, string endMatch) {
        int pFrom = Full.IndexOf(startMatch) + startMatch.Length;
        int pTo = Full.LastIndexOf(endMatch);
        if (pTo > pFrom)
            return Full.Substring(pFrom, pTo - pFrom);
        else
            return "";
    }

    public static void GenerateORMFiles() {
        string workingDirectory = EDMXworkingDirectory;
        if (!isEDMXAlreadyGenerated) {
            // Show Progress Bar here
            try {
                isEDMXAlreadyGenerated = true;
                Directory.CreateDirectory(@"C:\ERachana");
                Directory.CreateDirectory(@"C:\ERachana\EDMX");
                Directory.CreateDirectory(@"C:\ERachana\EDMX\EDMXFiles");
                Directory.CreateDirectory(workingDirectory);

                string CommandToCreateEDMXOnCommandLine = "\"%windir%\\Microsoft.NET\\Framework\\v4.0.30319\\edmgen.exe\" /mode:fullgeneration /c:\"data source = "
                                    + Server + "; initial catalog = "
                                    + Database + "; user id = "
                                    + UserName + "; password = "
                                    + Password + "; MultipleActiveResultSets = True; persist security info = True; App = EntityFramework\" /project:DataModel /entitycontainer:DBContext /namespace:Models /language:CSharp & exit";

                string ResourcesDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Resources\";
                string EDMXFileName = "DataModel.edmx";
                string ContextFileName = "DataModel.Context.tt";
                string TablesFileName = "DataModel.tt";

                string EdmxLocation = workingDirectory + @"\" + EDMXFileName;
                File.Copy(Path.Combine(ResourcesDirectory, EDMXFileName), EdmxLocation, true);
                File.Copy(Path.Combine(ResourcesDirectory, ContextFileName), workingDirectory + @"\" + ContextFileName, true);
                File.Copy(Path.Combine(ResourcesDirectory, TablesFileName), workingDirectory + @"\" + TablesFileName, true);
                using (var process = new Process()) {
                    var startInfo = new ProcessStartInfo {
                        WorkingDirectory = workingDirectory,
                        WindowStyle = ProcessWindowStyle.Minimized,
                        CreateNoWindow = true,
                        RedirectStandardInput = true,
                        UseShellExecute = false,
                        FileName = "cmd.exe",
                        Verb = "runas"
                    };

                    process.StartInfo = startInfo;
                    process.Start();
                    process.StandardInput.WriteLine(CommandToCreateEDMXOnCommandLine);
                    process.WaitForExit();
                    process.Close();
                    process.Dispose();
                }
                string text = File.ReadAllText(EdmxLocation);

                string c = "";
                c = parseSCMDLFiles(workingDirectory + @"\DataModel.ssdl", "Schema");
                text = text.Replace("###StorageModelsSchema", c);

                c = parseSCMDLFiles(workingDirectory + @"\DataModel.csdl", "Schema");
                text = text.Replace("###ConceptualModelsSchema", c);

                c = parseSCMDLFiles(workingDirectory + @"\DataModel.msl", "Mapping");
                text = text.Replace("###Mappings", c);

                File.WriteAllText(EdmxLocation, text);

                string[] fileToBeDeleted = Directory.GetFiles(workingDirectory);
                foreach (string filePath in fileToBeDeleted) {
                    if (filePath.Contains("DataModel.ObjectLayer.cs") || filePath.Contains("DataModel.Views.cs")) {
                        File.Delete(filePath);
                    } else {
                        if (filePath.ToLower().Contains(".edmx") || filePath.ToLower().Contains(".tt") || filePath.ToLower().Contains(".cs"))
                            continue;
                        File.Delete(filePath);
                    }
                }
                string location = @"C:\ERachana\EDMX";
                string TransformFileName = "transform_all.bat";
                File.Copy(Path.Combine(ResourcesDirectory, TransformFileName), location + @"\" + TransformFileName, true);
                string batFileCommand = "/C " + location + @"\" + TransformFileName;

                using (var process = new Process()) {
                    var startInfo = new ProcessStartInfo() {
                        WorkingDirectory = location,
                        WindowStyle = ProcessWindowStyle.Minimized,
                        CreateNoWindow = true,
                        UseShellExecute = false,
                        FileName = @"cmd.exe",
                        Verb = "runas",
                        Arguments = batFileCommand
                    };

                    process.StartInfo = startInfo;
                    process.Start();
                    process.WaitForExit();
                    process.Close();
                    process.Dispose();
                }
            } catch {
                MessageBox.Show("Only Projects with MSSQL may be converted to Web Projects");
            } finally {
                // Close Progressbar here
            }
        }
    }

    public static string parseSCMDLFiles(string EDMXDirectoryFile, string tag) {
        List<string> lines = File.ReadLines(EDMXDirectoryFile).ToList();
        string content = "";
        bool flagEnable = false;
        foreach (string line in lines) {
            if (line.Contains("</" + tag + ">"))
                flagEnable = false;
            if (flagEnable == true)
                content += line + Environment.NewLine;
            if (line.Contains("<" + tag))
                flagEnable = true;
        }
        return content;
    }
}

Short Answer简答

To make the edmx designer show the diagrams, you can use either of the following options:要使edmx设计器显示图表,您可以使用以下任一选项:

  • Having <Designers></Designers> tag in edmx file.edmx文件中有<Designers></Designers>标签。

  • Having .edmx.designer file with following contents and child of .edmx file:具有以下内容的.edmx.designer文件和.edmx文件的子文件:

     <?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx"> <edmx:Designer> <edmx:Diagrams> </edmx:Diagrams> </edmx:Designer> </edmx:Edmx>

Long Answer长答案

EdmGen.exe will not generate edmx for you, but it generates all data that you need to create an edmx file yourself. EdmGen.exe不会为您生成edmx ,但它会生成您自己创建edmx文件所需的所有数据。 You can simply create edmx file by mixing those csdl , ssdl and msl .您可以通过混合这些csdlssdlmsl来简单地创建 edmx 文件。

Also about the diagram file, you should know edmx.diagram file is not necessary.另外关于图表文件,您应该知道edmx.diagram文件不是必需的。 When you create the edmx file, with empty <Diagrams></Diagrams> tag, the first time that you open the edmx file in designer, Visual Studio will create the content for the tag for you.当您创建edmx文件,用空<Diagrams></Diagrams>标签,第一次打开在设计师的EDMX文件,Visual Studio将创建为您的标签内容。 Then if for any reason you like to have ot in separate file, you can simply right click on design surface of the edmx and choose Move Diagrams to Separate File .然后,如果出于任何原因您希望将 ot 放在单独的文件中,您只需右键单击 edmx 的设计图面,然后选择Move Diagrams to Separate File separate Move Diagrams to Separate File

You can follow the following steps to create an edmx file manually (or by code) yourself:您可以按照以下步骤自己手动(或通过代码)创建edmx文件:

1- Run EdmGen 1-运行EdmGen

"%windir%\Microsoft.NET\Framework\v4.0.30319\edmgen.exe" /mode:fullgeneration  /c:"Data Source=SERVERNAME; Initial Catalog=DATABASENAME;Integrated Security=SSPI" /project:PROJECT /entitycontainer:CONTAINER /namespace:NAMESPACE /language:CSharp /targetversion:4.5

2- Create an edmx file with following contents. 2-创建一个包含以下内容的 edmx 文件。

Please note that the edmx content which I used in this post, is based on /targetversion:4.5 switch.请注意,我在这篇文章中使用的edmx内容基于/targetversion:4.5开关。

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    $SSDL$
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
    $CSDL$
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
    $MSL$
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="true" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="true" />
        <DesignerProperty Name="UseLegacyProvider" Value="false" />
        <DesignerProperty Name="CodeGenerationStrategy" Value="None" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>

3- Replace the placeholders which you have in edmx with content of the following files (without <?xml version="1.0" encoding="utf-8"?> ): 3-将您在 edmx 中的占位符替换为以下文件的内容(没有<?xml version="1.0" encoding="utf-8"?> ):

  • $SSDL$ should be replaced with content of the ssdl file. $SSDL$应替换为ssdl文件的内容。
  • $CSDL$ should be replaced with content of the csdl file. $CSDL$应替换为csdl文件的内容。
  • $MSL$ should be replaced with content of the msl file. $MSL$应替换为msl文件的内容。

Note注意

.edmx.designer is optional and it's enough to have an <Diagrams></Diagrams> tag in edmx like what I shared above, then the first time that you open Visual Studio, the diagram will be created for you automatically. .edmx.designer是可选的,在edmx有一个<Diagrams></Diagrams>标签就edmx就像我上面分享的那样,那么当你第一次打开 Visual Studio 时,图表会自动为你创建。 Also for any reason if you like to have diagram ia separate file, you can simply create an empty diagram file, which will be filled by VS at the first time that you open edmx:同样出于任何原因,如果您喜欢单独的图表文件,您可以简单地创建一个空的图表文件,该文件将在您第一次打开 edmx 时由 VS 填充:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <edmx:Designer>
    <edmx:Diagrams>

    </edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>

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

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