2017-06-27 13 views
0

Dapper.FluentMap.Dommel.Mappingを使用している問題があります。マッピングを記録すると、システムは、名前IDのプロパティが既に存在し、例外をスローしたことを識別します。しかし、マップされたIDは別のオブジェクトに属します。どうすればこの問題を解決できますか?Dapper.FluentMap.Dommel.Mappingを複数のIDに使用する方法

BaseEntity.cs

public abstract class BaseEntity 
{ 
    public virtual long Id { get; set; } 
} 

Sistema.cs

public class Sistema : BaseEntity 
{   
    public override long Id { get; set; } 
    public string Nome { get; set; } 
} 

Arquivo.cs

public class Arquivo : BaseEntity 
{ 
    public override long Id { get; set; } 
    public Sistema Sistema { get; set; } 
    public Banco Banco { get; set; } 
    public List<Error> Erros { get; set; } 
    public string FullPath { get; set; } 
    public DateTime DtProcessamento { get; set; } 
    public int QtRegistros { get; set; } 
    public Decimal VlTotal { get; set; } 
    public int Sequencial { get; set; } 
    public bool isValid { get; set; } 
    public TipoComunicacao tipoComunicacao { get; set; } 
} 

ArquivoMap.cs

public class ArquivoMap : DommelEntityMap<Entities.Arquivo> 
{ 
    public ArquivoMap() 
    { 
     ToTable("Arquivo"); 

     Map(a => a.Id).ToColumn("arqu_id").IsKey(); 

     this.Map(a => a.Sistema.Id).ToColumn("sist_id"); //<-- Problm (1) 
     this.Map(a => a.Banco.Id).ToColumn("banc_id"); 
     //this.Map(a => a.Erros).ToColumn("erro_id"); // 
     this.Map(a => a.FullPath).ToColumn("arqu_nm_fullPath"); 
     this.Map(a => a.DtProcessamento).ToColumn("arqu_dt_processamento"); 
     this.Map(a => a.QtRegistros).ToColumn("arqu_qt_registros"); 
     this.Map(a => a.VlTotal).ToColumn("arqu_vl_total"); 
     this.Map(a => a.Sequencial).ToColumn("arqu_id_sequencial"); 
     this.Map(a => a.isValid).ToColumn("arqu_bt_valid"); 
     this.Map(a => a.tipoComunicacao).ToColumn("arqu_cd_comunicacao"); 
    } 
} 

RegisterMappings.cs

public static void Register() 
{ 
    FluentMapper.Initialize(config => 
    { 
     config.AddMap(new ArquivoMap()); //<-- Call to map 
     config.AddMap(new BancoMap()); 
     config.AddMap(new ErrorMap()); 
     config.AddMap(new SistemaMap()); 
     config.ForDommel(); 
    }); 
} 

エラー:

System.Exception: 'Duplicate mapping detected. Property 'Id' is already mapped to column 'Id'.'

答えて

1

あなたが最初にあなたはArquivoMapクラスでシステマのプロパティを無視する必要があり、個別の各クラスをマップする必要があります:

this.Map(a => a.Sistema).Ignore(); 

次に、Sistemaクラスのマップを持つ別のクラスを作成します。

public class SistemaMap : DommelEntityMap<Entities.Sistema>{ 
    public SistemaMap() 
    { 
     ToTable("Sistema");   
     this.Map(a => a.Id).ToColumn("sist_id"); 
    } 
} 

クラスのすべてのサブオブジェクトで実行します。

var arquivoList= yourConnection.Query<Arquivo,Sistema, Arquivo>(sqlQuery, 
(arquivo, sistema)=>{ 
     arquivo.Sistema= sistema; 
     return arquivo; 
    }); 
    return arquivoList; 

このリンクで複数の問合せを行う方法の詳細を確認します:http://dapper-tutorial.net/result-multi-mapping

あなたが参加している場合はマッピングオブジェクトがデータベースからマップを取得する - そして、あなたが複数の問合せを行う必要があり、クエリが作りますあなたのクエリでsplitOnプロパティを使う必要があるかもしれません。この投稿では非常によくそれを行う方法を掘り下げます: Correct use of Multimapping in Dapper

私はこれが助けてくれることを望みます。

関連する問題