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 ファイルを生成する方法に関するドキュメントも公式ドキュメントから読みました。
edmx.document ファイルを生成するためのドキュメントが Microsoft のドキュメントに記載されていないため、自分で解決策を見つけることができませんでした。私はこの問題でかなり長い間立ち往生しており、これを解決するための助けが必要です.
同様のメカニズムを使用して、SQL2LINQコンバーター プロジェクトで必要なファイルを生成しました。この機能があれば、非常に役立ちます。私を助けてください。
編集 1: edmx.diagram ファイルにそのようなプロパティがあることに気付きました。Visual Studio が他の実行可能ファイルを内部的に使用してダイアグラム ファイルを生成するかどうか、またはコマンド ライン経由でダイアグラム ファイルを作成できる文書化されていないフラグがあるかどうかはわかりません。最初に質問を投稿したときに、この情報を編集できなかったことをご容赦ください。
編集2: 私が使用するプロセスに含まれるすべてのステップ:
ステップ 1 :リソース ファイルを、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;
}
}