外键从同一个表接收两个主键 - Fluent Nhibernate

问题描述 投票:0回答:1

我在尝试创建一个复合 ID 时遇到问题,该 ID 从一个表接收一个外键,从另一个表接收另一个外键,但是第二个外键包含两个主键,这让我很头疼。有谁知道如何解决这个问题吗?

这是我的代码:

实体

public class GrupoArquivo
{
    public GrupoArquivo() {}

    public GrupoArquivo(ArquivoRetorno arquivoRetorno, GrupoModulo grupo, GrupoModulo modulo) : this()
    {
        Arquivo = arquivoRetorno;
        Grupo = grupo;
        Modulo = modulo;
    }

    public virtual ArquivoRetorno Arquivo { get; protected set; }
    public virtual GrupoModulo Grupo { get; protected set; }
    public virtual GrupoModulo Modulo { get; protected set; }

    public override bool Equals(object obj)
    {
        var grupoArquivo = (obj as GrupoArquivo);

        if (grupoArquivo != null)
        {
            if (ReferenceEquals(obj, this))
                return true;

            var thisHash = GetHashCode();
            var otherHash = grupoArquivo.GetHashCode();

            return thisHash.Equals(otherHash);
        }
        return false;
    }

    public override int GetHashCode()
    {
        return string.Concat("{0}|{1}|{2}", Arquivo, Grupo, Modulo).GetHashCode();
    }
}

映射

 public class GrupoArquivoMap : ClassMap<GrupoArquivo>
{
    public GrupoArquivoMap()
    {
        Schema(Const.SCHEMA);
        Table(Const.TB_EMAIL_GRUPO_ARQUIVO);

        CompositeId()
            .KeyReference(x => x.Arquivo, Const.ID_ARQUIVO)
            .KeyReference(x => x.Grupo, Const.ID_GRUPO)
            .KeyReference(x => x.Modulo, Const.ID_MODULO)
            ;
    }
}
c# nhibernate fluent-nhibernate
1个回答
1
投票

我解决了它,它非常“简单”,我只需在实体中引用每个表一次,并且在映射中我定义了来自同一个“KeyReference”上的同一个表的两列。

实体

    public GrupoArquivo() {}

    public GrupoArquivo(ArquivoRetorno arquivoRetorno, GrupoModulo grupoModulo) : this()
    {
        Arquivo = arquivoRetorno;
        GrupoModulo = grupoModulo;
    }

    public virtual ArquivoRetorno Arquivo { get; protected set; }
    public virtual GrupoModulo GrupoModulo { get; protected set; }

    public override bool Equals(object obj)
    {
        var grupoArquivo = (obj as GrupoArquivo);

        if (grupoArquivo != null)
        {
            if (ReferenceEquals(obj, this))
                return true;

            var thisHash = GetHashCode();
            var otherHash = grupoArquivo.GetHashCode();

            return thisHash.Equals(otherHash);
        }
        return false;
    }

    public override int GetHashCode()
    {
        return string.Concat("{0}|{1}|{2}", Arquivo, GrupoModulo).GetHashCode();
    }

映射

public class GrupoArquivoMap : ClassMap<GrupoArquivo> 
{
    public GrupoArquivoMap()
    {
        Schema(Const.SCHEMA);
        Table(Const.TB_EMAIL_GRUPO_ARQUIVO);

        CompositeId()
            .KeyReference(x => x.Arquivo, Const.ID_ARQUIVO)
            .KeyReference(x => x.GrupoModulo, Const.ID_GRUPO, Const.ID_MODULO)
            ;
    }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.