Using the suggestion provided in GitHub, I was able to generate the EDMX files needed for an ASP.net project. 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.
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:
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.
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. 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. Having this capability could help tremendously. Please help me.
Edit 1: I have noticed that the edmx.diagram file has property like so. 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. 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:
Step1: Copy my resource files to the folder where I require my edmx and dependency files to be generated.
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.
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. the files are then read and replaced int he edmx files that i have included in the resources folder in the link above.
Step 4: Run The textTransform.bat file to run the texttransform.exe from the Windows SDK path for Texttransform.exe.
Observation: At this stage, 5 of the 6 files are created namely:
- .context.tt
- .context.cs
- .Designer.cs
- .tt
- .cs
corresponding to the name provided by the user.
But the file .edmx.diagram is missing.
Code that does step 1 through 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;
}
}