[英]Foreign key receiving two primary keys from the same table - Fluent Nhibernate
I am having a problem trying to create a Composite Id that receives one foreign key from one table and another foreign key from another table, but this second foreign key contains two primary keys and this giving my a big headache to resolve. 我在尝试创建一个从一个表中接收一个外键并从另一个表中接收另一个外键的组合ID时遇到问题,但是第二个外键包含两个主键,这让我很难解决。 Anyone knows how to resolve this problem?
有人知道如何解决这个问题吗?
Here is my code: 这是我的代码:
Entity 实体
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();
}
}
Mapping 制图
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)
;
}
}
I resolved it and it was quite "simple", I had to reference each table only once in the entity and in the mapping I defined the two collumns coming from the same table on the same "KeyReference". 我解决了这个问题,这非常“简单”,我只需要在实体中引用每个表一次,并且在映射中,我定义了来自同一表上同一“ KeyReference”上的两个列。
Entity 实体
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();
}
Mapping 制图
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)
;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.