使用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,那么我不知道如何生成在Visual中生成的附带的edmx.diagram文件。
在解决方案资源管理器中,可以在如下位置看到该文件:

还可以在Visual中打开该文件,以UML图的形式查看数据库结构,如下所示:

此外,为此生成的文件如下所示:

我阅读了关于如何使用edmgen.exe从官方文档生成edmx文件的文档。
我相信生成edmx.document文件的文档在微软文档中被遗漏了,我无法自己想出一个解决方案。我在这个问题上被困了很长一段时间,在解决这个问题上需要帮助。
我使用了类似的机制来生成SQL2LINQ转换器项目所需的文件。拥有这种能力可能会有很大帮助。请帮帮我。
编辑1:我注意到edmx.diagram文件具有类似的属性。我不确定的是Visual内部是否使用其他可执行文件来生成关系图文件,或者是否有一个无文档的标志可以通过命令行创建图表文件。请原谅我的编辑这一信息是不平等的,而最初发布的问题。

编辑2:我使用的流程所涉及的所有步骤:
Step1:将资源文件复制到需要生成edmx和依赖文件的文件夹中。
注意:这些文件是虚拟文件,将由我在问题中粘贴的命令行生成。
步骤2:通过导航到相同的路径来运行命令行命令。
步骤3:运行命令行后,从用户收集的连接字符串将有助于在同一个目录中生成必要的CSDL、SSDL和MSL文件。然后读取并替换我已包含在上面链接的“资源”文件夹中的included文件。
步骤4:运行textTransform.bat文件,从Windows路径为Texttransform.exe运行texttransform.exe。
在这个阶段,Observation:创建了6个文件中的5个,即:
对应于用户提供的名称。
但是文件.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;
}
}发布于 2018-01-25 06:37:08
短答案
要使edmx设计器显示图表,您可以使用以下选项之一:
<Designers></Designers>文件中有edmx标记。.edmx.designer文件和.edmx文件的子文件:长答案
EdmGen.exe不会为您生成edmx,但是它会生成您自己创建edmx文件所需的所有数据。您可以简单地通过混合这些csdl、ssdl和msl来创建edmx文件。
另外,关于图文件,您应该知道edmx.diagram文件是不必要的。当您使用空的edmx标记创建<Diagrams></Diagrams>文件时,当您第一次在designer中打开edmx文件时,Visual将为您创建标记的内容。然后,如果出于任何原因,您喜欢在单独的文件中使用ot,只需右键单击edmx的设计图面并选择Move Diagrams to Separate File。
您可以按照以下步骤手动(或通过代码)自己创建edmx文件:
1- Run 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.52-创建包含以下内容的edmx文件。
请注意,我在这篇文章中使用的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文件的内容。Note
.edmx.designer是可选的,在edmx中有一个<Diagrams></Diagrams>标记就足够了,就像我上面共享的一样,然后当您第一次打开Visual时,图表将自动为您创建。此外,由于任何原因,如果您想让图i成为一个单独的文件,您可以简单地创建一个空图文件,在打开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>https://stackoverflow.com/questions/48354180
复制相似问题