そのため、ID 値の説明/名前である ID 列 (int) を格納するデータベースに既存のルックアップ テーブルがあります。この表のデータは、列挙の目的で使用されています。
その後、Entity Framework 5 と CodeFirst の使用に移行しましたが、ルックアップ値はまだデータベースにあります。やりたいことは、値を表すエンティティを作成することです。できれば、そのテーブルの値と名前にアクセスし、アプリケーションで使用できるようにする列挙型を作成します。
通常、以下のコードは、データベース値から列挙型を動的に構築するのに意味がありますが、これを避けたいと思います。
static void GenerateStateEnumerations(String enumassemblyName, String enumTypeName, String assemblyName, IDictionary<int, String> EnumDB)
{
// Get the current application domain for the current thread.
AppDomain currentDomain = AppDomain.CurrentDomain;
// Create a dynamic assembly in the current application domain,
// and allow it to be executed and saved to disk.
AssemblyName EnumassemblyName = new AssemblyName(enumassemblyName);
AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
EnumassemblyName, AssemblyBuilderAccess.RunAndSave);
// Define a dynamic module in assemblyName assembly. For a single-
// module assembly, the module has the same name as the assembly.
ModuleBuilder mb = ab.DefineDynamicModule(EnumassemblyName.Name, EnumassemblyName.Name + ".dll");
// Define a public enumeration with the name "Elevation" and an
// underlying type of Integer.
EnumBuilder eb = mb.DefineEnum(enumTypeName, TypeAttributes.Public, typeof(int));
// Define the enumeration members
foreach (KeyValuePair<int, String> element in EnumDB)
{
eb.DefineLiteral(element.Value, element.Key);
}
// Create the type and save the assembly.
Type finished = eb.CreateType();
//Save the assembly via the assembly builder
ab.Save(EnumassemblyName.Name + ".dll");
}
EF 5 で、このテーブルに格納されているデータを表す列挙エンティティであるエンティティを作成するにはどうすればよいですか? これは現在可能ですか?