使用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
但是,如果我们通过 ADO.Net 数据模型添加到现有项目中创建 EDMX,我不知道如何生成在 Visual Studio 中生成的随附edmx.diagram文件。
该文件也可以在 Visual Studio 中打开,以 UML 图的形式查看数据库结构,如下所示:
我也从官方文档中阅读了有关如何使用 edmgen.exe 生成 edmx 文件的文档。
我相信 Microsoft 文档中遗漏了生成 edmx.document 文件的文档,我自己无法为此提出解决方案。我在这个问题上被困了很长时间,需要帮助来解决这个问题。
我使用了类似的机制来生成SQL2LINQ转换器项目中所需的文件。拥有这种能力可以提供极大的帮助。请帮我。
编辑 1: 我注意到 edmx.diagram 文件具有这样的属性。我不确定Visual Studio是否在内部使用其他一些可执行文件来生成图表文件,或者是否有一个未记录的标志可以通过命令行创建图表文件。请原谅我在最初发布问题时编辑此信息不可用。
编辑2: 我使用的过程中涉及的所有步骤:
Step1:将我的资源文件复制到我需要生成我的 edmx 和依赖文件的文件夹中。
注意:这些文件是虚拟文件,将从我粘贴在问题中的命令行命令生成。
第 2 步:通过导航到相同的路径运行命令行命令。
步骤 3:命令行运行后,从用户那里收集的连接字符串将有助于在同一目录中生成必要的 CSDL、SSDL 和 MSL 文件。然后读取文件并将其替换为我已包含在上面链接的资源文件夹中的 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;
}
}