如何对多个ID使用Dapper.FluentMap.Dommel.Mapping [英] How to use Dapper.FluentMap.Dommel.Mapping for multiple Ids

查看:467
本文介绍了如何对多个ID使用Dapper.FluentMap.Dommel.Mapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Dapper.FluentMap.Dommel.Mapping时遇到问题.当我记录映射时,系统会识别出名称ID为ID的属性已经存在,并引发异常.但是映射的ID属于另一个对象.我该如何解决这个问题?

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

BaseEntity.cs

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

Sistema.cs

Sistema.cs

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

Arquivo.cs

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

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

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:'检测到重复的映射.属性"Id"已经映射到列"Id".

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

推荐答案

您需要分别映射每个类,首先需要忽略ArquivoMap类中Sistema的属性:

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

然后使用Sistema类的地图创建一个单独的类:

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

对类的每个子对象执行此操作.然后,要进行查询,您需要执行"MultiQuery-映射"以从数据库中获取映射的对象:

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;

在此链接中检查有关如何进行多重查询的详细信息: http://dapper-tutorial. net/result-multi-mapping

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

如果查询中有联接,则可能需要使用splitOn属性.在这篇文章中,很好地解释了如何做到这一点: 在Dapper中正确使用多重映射

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.

这篇关于如何对多个ID使用Dapper.FluentMap.Dommel.Mapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆