SO に関する多数の投稿に基づいて、AutoMapper の拡張メソッドを作成して、すべての EF 4.0 ビジネス オブジェクトに対して、すべてのタイプの EF 関連プロパティをEntityReference<T>
マッピングEntityCollection<T>
から除外しようとしています。
私はこのコードを思いつきました:
public static class IgnoreEfPropertiesExtensions {
public static IMappingExpression<TSource, TDestination> IgnoreEfProperties<TSource, TDestination>(
this IMappingExpression<TSource, TDestination> expression) {
var sourceType = typeof (TSource);
foreach (var property in sourceType.GetProperties()) {
// exclude all "EntityReference" and "EntityReference<T>" properties
if (property.PropertyType == typeof(EntityReference) ||
property.PropertyType.IsSubclassOf(typeof(EntityReference)))
{
expression.ForMember(property.Name, opt => opt.Ignore());
}
// exclude all "EntityCollection<T>" properties
if (property.PropertyType == typeof(EntityCollection<>))
{
expression.ForMember(property.Name, opt => opt.Ignore());
}
}
return expression;
}
}
しかし、どういうわけか、EntityReference<T>
プロパティはうまく機能しますが、コレクションの場合、これはすべきことを行いません.AutoMapperはまだマッピングEntityCollection<MySubEntity>
を試み、これを試みるとクラッシュします....
EntityCollection<T>
すべてのプロパティを適切にキャッチするにはどうすればよいですか? サブタイプが何であるかはあまり気にしません。<T>
指定したくありません。 (コレクションに含まれるものに関係なく) タイプのすべてのプロパティをEntityCollection<T>
マッピングから除外する必要があります。
何か案は?