C# 如何使用edmgen.exe生成.edmx.diagram文件?
使用中提供的建议,我能够生成ASP.net项目所需的EDMX文件。使用如下命令:C# 如何使用edmgen.exe生成.edmx.diagram文件?,c#,asp.net-mvc,entity-framework,entity-framework-6,edmx-designer,C#,Asp.net Mvc,Entity Framework,Entity Framework 6,Edmx Designer,使用中提供的建议,我能够生成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:School
"%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
但是,如果我们通过ADO.Net数据模型在现有项目之外创建edmx,我不知道如何生成在Visual Studio中生成的附带的edmx.diagram文件
在解决方案资源管理器中,可以在如下位置查看文件:
也可以在Visual studio中打开该文件,以UML图的形式查看数据库结构,如下所示:
此外,为此生成的文件如下所示:
我阅读了有关如何使用edmgen.exe生成edmx文件的文档,以及来自官方的文档
我相信Microsoft文档中缺少生成edmx.document文件的文档,我自己无法想出解决方案。我在这个问题上已经被困了很长时间,需要帮助解决这个问题
我使用了类似的机制来生成convertor项目所需的文件。拥有这一能力可以大有帮助。请帮帮我
编辑1:
我注意到edmx.diagram文件具有这样的属性。我不确定的是,VisualStudio是否在内部使用其他一些可执行文件来生成图表文件,或者是否存在可以通过命令行创建图表文件的未记录标志。请原谅我的编辑此信息在最初发布问题时不可用
编辑2:
我使用的流程中涉及的所有步骤:
步骤1:将我的文件复制到需要生成edmx和依赖项文件的文件夹中
注意:这些文件是虚拟文件,将由我在问题中粘贴的命令行命令生成
步骤2:通过导航到同一路径来运行命令行命令
步骤3:运行命令行后,从用户收集的连接字符串将帮助在同一目录中生成必要的CSDL、SSDL和MSL文件。然后,这些文件将被读取并替换到edmx文件中,我已将这些文件包含在上面链接的resources文件夹中
步骤4:运行textTransform.bat文件,从textTransform.exe的Windows SDK路径运行textTransform.exe
观察:
在此阶段,将创建6个文件中的5个,即:
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;
}
}
内部类全局变量{
公共静态字符串EDMXworkingDirectory=@“C:\ERachana\EDMX\EDMXFiles\EDMXParts”;
公共静态布尔值isEDMXAlreadyGenerated=false;
公共静态字符串服务器=”,数据库=”,用户名=”,密码=”;
公共静态字符串ProjectName=“”,UserDefinedObjectName=“\u appdb”,TemporaryDirectoryPath=“”;
公共静态字符串GetSubstringBetween字符串(字符串已满、字符串开始匹配、字符串结束匹配){
int pFrom=完整的索引(startMatch)+startMatch.Length;
int pTo=满。最后索引Of(endMatch);
如果(pTo>pFrom)
返回满。子串(pFrom,pTo-pFrom);
其他的
返回“”;
}
公共静态void GenerateORMFiles(){
字符串workingDirectory=EDMXworkingDirectory;
如果(!isEDMXAlreadyGenerated){
//在此显示进度条
试一试{
isEDMXAlreadyGenerated=true;
CreateDirectory(@“C:\ERachana”);
CreateDirectory(@“C:\ERachana\EDMX”);
CreateDirectory(@“C:\ERachana\EDMX\EDMXFiles”);
目录.CreateDirectory(工作目录);
string COMMANDTOCREATEDMXONCOMMANDLINE=“\%windir%\\Microsoft.NET\\Framework\\v4.0.30319\\edmgen.exe\”/mode:fullgeneration/c:\“数据源=”
+服务器+“初始目录=”
+数据库+“用户id=”
+用户名+“密码=”
+Password+“MultipleActiveResultSets=True;persist security info=True;App=EntityFramework\”/project:DataModel/entitycontainer:DBContext/namespace:Models/language:CSharp&exit”;
字符串ResourcesDirectory=Path.GetDirectoryName(Assembly.getExecutionGassembly().Location)+@“\Resources\”;
字符串EDMXFileName=“DataModel.edmx”;
string ContextFileName=“DataModel.Context.tt”;
string tablefilename=“DataModel.tt”;
字符串EdmxLocation=workingDirectory+@“\”+EDMXFileName;
Copy(Path.Combine(ResourcesDirectory,EDMXFileName),EdmxLocation,true);
File.Copy(Path.Combine(ResourcesDirectory,ContextFileName),workingDirectory+@“\”+ContextFileName,true);
File.Copy(Path.Combine(ResourcesDirectory,tablefilename),workingDirectory+@“\”+tablefilename,true);
使用(var进程=新进程()){
var startInfo=新流程startInfo{
WorkingDirectory=WorkingDirectory,
WindowsStyle=ProcessWindowsStyle.Minimized,
CreateNoWindow=true,
重定向标准输入=真,
UseShellExecute=false,
FileName=“cmd.exe”,
动词=“符文”
};
process.StartInfo=StartInfo;
process.Start();
process.StandardInput.WriteLine(CommandToCreatedMXonCommandLine);
process.WaitForExit();
公共关系
<?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>