How to use Dapper.FluentMap.Dommel.Mapping for multiple Ids

早过忘川 提交于 2019-12-12 05:18:13

问题


I'm having a problem using Dapper.FluentMap.Dommel.Mapping. When I log the mapping, the system identifies that a property with the name ID already exists and throws the exception. But mapped Id belongs to another object. How can I solve this problem?

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();
    });
}

Error:

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


回答1:


You need to map each class separate, first you need to ignore the property of Sistema in your ArquivoMap class:

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

Then create a separate class with the map for Sistema class:

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

Do it with every sub object of the class. Then to make the query you need to do a MultiQuery - Mapping to get the object mapped from the database:

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

Check the details how to do multiquery in this link:http://dapper-tutorial.net/result-multi-mapping

If you have a join in your query maybe you need to use the splitOn property. In this post explaint very well how to do it: Correct use of Multimapping in Dapper

I hope this can help.



来源:https://stackoverflow.com/questions/44789056/how-to-use-dapper-fluentmap-dommel-mapping-for-multiple-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!