エンティティにタイプエンティティのフィールドがある場合、Automapperの使用方法を理解しようとしています。
私はこれらのような3つのクラス持っている:文字列値をAutomapperのエンティティに変換します
public abstract class Entity<IdK>
{
public virtual IdK Code { get; protected set; }
}
public class Contact : Entity
{
public virtual string Name { get; set; }
public virtual Company Company { get; set; }
}
public class Company : Entity
{
public virtual string Name { get; set; }
}
私のクラスの連絡先は、型企業の要素が含まれています。
は、私も自分のビューにいくつかに関する情報をtrasferするのViewModelを作成しました:
public ContactViewModel()
{
public Guid Code { get; set; }
public int Version { get; set; }
[DisplayName("Contact")]
public string Name { get; set; }
[DisplayName("Company")]
public string Company { get; set; }
}
を私のviewmodelで、私は、文字列型のフィールドの会社を定義しました。これは、ユーザーがドロップダウン(企業のリスト)から選択する値を含む予定です。
私のアプリが起動したとき、私はブートストラップを定義した:
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x => {
x.CreateMap<Domain.Contact, ViewModels.ContactViewModel>();
x.CreateMap<ViewModels.ContactViewModel, Domain.Contact>()
});
}
}
私はエンティティに私のViewModelを再マップしようとすると、私は変換エラー(AutoMapper.AutoMapperMappingException)を取得します。 Automapperは私の会社(文字列)をCompany、Contactのメンバーのオブジェクトに変換する方法を理解できません。 Automapperが文字列(会社)を私のCompanyオブジェクト、Contactのメンバーのコードに変換する方法を知るようにルールを定義することは可能ですか?
:ような何か:
次にマッピングコードにあなたのようにそれを呼び出します。ありがとう。 – LeftyX