1

Guid?ビュー モデルにマップする Code First データ モデルに、null 非許容の GUID プロパティがいくつかあります。空の GUID (すべてゼロ) は使用しないので、次のマッピングを使用しますが、これを行うより適切な方法があるかどうか疑問に思わずにはいられません。AutoMapper 構成の未知の深さは、すべてを自分で調査するには何年もかかるでしょう。

Mapper.CreateMap<Guid, Guid?>().ConvertUsing(guid => guid == Guid.Empty ? (Guid?)null : guid);
Mapper.CreateMap<Guid?, Guid>().ConvertUsing(guid => !guid.HasValue ? Guid.Empty : guid.Value);
4

1 に答える 1

1

カスタムタイプコンバーターを作成します。

https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

 public class NullableByteToNullableIntConverter : ITypeConverter<Byte?, Int32?>
    {
        public Int32? Convert(ResolutionContext context)
        {
            return context.IsSourceValueNull ? (int?) null : System.Convert.ToInt32(context.SourceValue);
        }
    }

それで:

  Mapper.CreateMap<byte?, int?>().ConvertUsing<NullableByteToNullableIntConverter>();
于 2012-04-23T18:31:44.070 に答える