如何使用 edmgen.exe 生成 .edmx.diagram 文件?
How to generate .edmx.diagram file using edmgen.exe?
使用 GitHub 中提供的建议,我能够生成 ASP.net 项目所需的 EDMX 文件。使用如下命令:
"%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
但我不知道如何生成随附的 edmx.diagram 文件,如果我们通过 [=86 创建 EDMX,则在 Visual Studio 中生成该文件=] 数据模型添加到现有项目。
在解决方案资源管理器中,可以在如下位置看到该文件:
也可以在 Visual studio 中打开该文件,以 UML 图的形式查看数据库结构,如下所示:
此外,为此生成的文件显示如下:
我也从官方 documentation 阅读了有关如何使用 edmgen.exe 生成 edmx 文件的文档。
我认为 Microsoft 文档中遗漏了生成 edmx.document 文件的文档,我自己无法为此提出解决方案。我在这个问题上被困了很长一段时间,需要帮助来解决这个问题。
我使用了类似的机制来生成 SQL2LINQ 转换器项目中所需的文件。拥有这种能力可能会大有帮助。请帮助我。
编辑 1:
我注意到 edmx.diagram 文件有这样的 属性。我不确定 Visual Studio 是否在内部使用其他一些可执行文件来生成图表文件,或者是否有一个未记录的标志可以通过命令行创建图表文件。请原谅我 编辑 在最初发布问题时此信息不可用。
编辑 2:
我使用的过程中涉及的所有步骤:
第一步:将我的resource files复制到我需要生成edmx和依赖文件的文件夹中。
注意:这些文件是虚拟文件,将从我粘贴在问题中的命令行命令生成。
第 2 步:运行 通过导航到同一路径的命令行命令。
第三步:命令行运行后,从用户那里收集的连接字符串将有助于在同一目录中生成必要的CSDL、SSDL和MSL文件。然后读取文件并将其替换为我在上面 link 的资源文件夹中包含的 edmx 文件。
第 4 步:运行 textTransform.bat 文件到 运行 texttransform.exe 从 Windows SDK 路径 Texttransform.exe.
观察:
在此阶段,将创建 6 个文件中的 5 个,即:
- .context.tt
- .context.cs
- .Designer.cs
- .tt
- .cs
对应用户提供的名称
但是文件 .edmx.diagram 丢失了。
执行第 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;
}
}
简答
要使 edmx
设计器显示图表,您可以使用以下任一选项:
在 edmx
文件中有 <Designers></Designers>
标签。
.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>
长答案
EdmGen.exe
不会为您生成 edmx
,但它会生成您自己创建 edmx
文件所需的所有数据。您可以通过混合 csdl
、ssdl
和 msl
.
来简单地创建 edmx 文件
关于图表文件,您应该知道 edmx.diagram
文件不是必需的。当您创建带有空 <Diagrams></Diagrams>
标签的 edmx
文件时,您第一次在设计器中打开 edmx 文件时,Visual Studio 将为您创建标签的内容。然后,如果出于任何原因您希望将 ot 放在单独的文件中,您只需右键单击 edmx 的设计图面并选择 Move Diagrams to Separate File
。
您可以按照以下步骤自己手动(或通过代码)创建 edmx
文件:
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- 创建一个包含以下内容的 edmx 文件。
请注意,我在此 post 中使用的 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- 将您在 edmx 中的占位符替换为以下文件的内容(没有 <?xml version="1.0" encoding="utf-8"?>
):
$SSDL$
应替换为 ssdl
文件的内容。
$CSDL$
应替换为 csdl
文件的内容。
$MSL$
应替换为 msl
文件的内容。
备注
.edmx.designer
是可选的,在 edmx
中有一个 <Diagrams></Diagrams>
标签就足够了,就像我上面分享的那样,然后你第一次打开 Visual Studio,图表将自动为您创建。同样出于任何原因,如果您想将图表放在一个单独的文件中,您可以简单地创建一个空图表文件,VS 将在您第一次打开 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>
使用 GitHub 中提供的建议,我能够生成 ASP.net 项目所需的 EDMX 文件。使用如下命令:
"%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
但我不知道如何生成随附的 edmx.diagram 文件,如果我们通过 [=86 创建 EDMX,则在 Visual Studio 中生成该文件=] 数据模型添加到现有项目。
在解决方案资源管理器中,可以在如下位置看到该文件:
也可以在 Visual studio 中打开该文件,以 UML 图的形式查看数据库结构,如下所示:
此外,为此生成的文件显示如下:
我也从官方 documentation 阅读了有关如何使用 edmgen.exe 生成 edmx 文件的文档。
我认为 Microsoft 文档中遗漏了生成 edmx.document 文件的文档,我自己无法为此提出解决方案。我在这个问题上被困了很长一段时间,需要帮助来解决这个问题。
我使用了类似的机制来生成 SQL2LINQ 转换器项目中所需的文件。拥有这种能力可能会大有帮助。请帮助我。
编辑 1: 我注意到 edmx.diagram 文件有这样的 属性。我不确定 Visual Studio 是否在内部使用其他一些可执行文件来生成图表文件,或者是否有一个未记录的标志可以通过命令行创建图表文件。请原谅我 编辑 在最初发布问题时此信息不可用。
编辑 2: 我使用的过程中涉及的所有步骤:
第一步:将我的resource files复制到我需要生成edmx和依赖文件的文件夹中。
注意:这些文件是虚拟文件,将从我粘贴在问题中的命令行命令生成。
第 2 步:运行 通过导航到同一路径的命令行命令。
第三步:命令行运行后,从用户那里收集的连接字符串将有助于在同一目录中生成必要的CSDL、SSDL和MSL文件。然后读取文件并将其替换为我在上面 link 的资源文件夹中包含的 edmx 文件。
第 4 步:运行 textTransform.bat 文件到 运行 texttransform.exe 从 Windows SDK 路径 Texttransform.exe.
观察: 在此阶段,将创建 6 个文件中的 5 个,即:
- .context.tt
- .context.cs
- .Designer.cs
- .tt
- .cs
对应用户提供的名称
但是文件 .edmx.diagram 丢失了。
执行第 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;
}
}
简答
要使 edmx
设计器显示图表,您可以使用以下任一选项:
在
edmx
文件中有<Designers></Designers>
标签。.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>
长答案
EdmGen.exe
不会为您生成 edmx
,但它会生成您自己创建 edmx
文件所需的所有数据。您可以通过混合 csdl
、ssdl
和 msl
.
关于图表文件,您应该知道 edmx.diagram
文件不是必需的。当您创建带有空 <Diagrams></Diagrams>
标签的 edmx
文件时,您第一次在设计器中打开 edmx 文件时,Visual Studio 将为您创建标签的内容。然后,如果出于任何原因您希望将 ot 放在单独的文件中,您只需右键单击 edmx 的设计图面并选择 Move Diagrams to Separate File
。
您可以按照以下步骤自己手动(或通过代码)创建 edmx
文件:
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- 创建一个包含以下内容的 edmx 文件。
请注意,我在此 post 中使用的 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- 将您在 edmx 中的占位符替换为以下文件的内容(没有 <?xml version="1.0" encoding="utf-8"?>
):
$SSDL$
应替换为ssdl
文件的内容。$CSDL$
应替换为csdl
文件的内容。$MSL$
应替换为msl
文件的内容。
备注
.edmx.designer
是可选的,在 edmx
中有一个 <Diagrams></Diagrams>
标签就足够了,就像我上面分享的那样,然后你第一次打开 Visual Studio,图表将自动为您创建。同样出于任何原因,如果您想将图表放在一个单独的文件中,您可以简单地创建一个空图表文件,VS 将在您第一次打开 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>