私が達成しようとしているのは、私が生成した C# クラスからプロジェクトを動的に生成することです。このクラスの内容は、エンティティ フレームワークのコード ファースト コード生成の同様の内容です。内容は次のようになります。
namespace ElasticTables
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.KeyAttribute;
[Table("address")]
public partial class address
{
[Key]
public decimal id { get; set; }
public string name { get; set; }
}
}
データベース内のテーブルからこのファイルを生成し、プログラムでコンパイルして、生成されたプロジェクトを API で動作する別のプロジェクトで参照できるようにします。
コンパイル中の主なエラーは次のとおりです。
タイプまたは名前空間名「KeyAttribute」が名前空間「System.ComponentModel.DataAnnotations」に存在しません (アセンブリ参照がありませんか?)
タイプまたはネームスペース「キー」が見つかりませんでした
タイプまたはネームスペース「テーブル」が見つかりませんでした。
「CSharpCodeProvider」を使用しています
var provider = new CSharpCodeProvider();
var options = new CompilerParameters
{
OutputAssembly = "ElasticTables.dll",
CompilerOptions = "/optimize"
};
そして、私は次の参照されたアセンブリを持っています
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.dll");
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.SqlServer.dll");
ソースと呼ばれるファイルのパスを含む文字列配列があり、次の行でコンパイルしようとしています
CompilerResults results = provider.CompileAssemblyFromFile(options, sources);
助けていただければ幸いです。