1

エンティティ フレームワークを使用して、クラス内のデータベース内のクラスをマップしますObjectContext

 var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
            .Where(type => !String.IsNullOrEmpty(type.Namespace))
            .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }

            //Replace with ....
            //modelBuilder.Configurations.Add(new EntityMap());

上記のコードはすべてのクラスを取得しますtypeof(EntityTypeConfiguration<>)。私の「EntityMap」クラスが私のオブジェクト コンテキストと同じクラス ライブラリにある場合は問題map classesありませんが、別のクラス ライブラリに書いた場合は機能しません。Map classesで識別できませんObjectContext

4

2 に答える 2

0
var assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (var assembly in assemblies)
{ 
    assembly.GetTypes().Where(type => !String.IsNullOrEmpty(type.Namespace))
                       .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
    foreach (var type in typesToRegister)
    {
        dynamic configurationInstance = Activator.CreateInstance(type);
        modelBuilder.Configurations.Add(configurationInstance);
    }
}

これにより、現在のアセンブリだけでなく、読み込まれたすべてのアセンブリが取得されます。

于 2014-03-01T19:30:46.197 に答える
0

これを次のように変更することをお勧めします。

var typesToRegister = typeof(MapClass).Assembly.GetTypes()
                      .....

whereは、他のアセンブリにあるMapClassから継承するクラスの 1 つです。EntityTypeConfiguration<T>

于 2014-03-01T19:25:05.053 に答える