2

我有一个使用 fluent nhibernate 的复合键的映射配置,我需要一些帮助,因为它不起作用。

我有一个名为 Agencia 的类,它有一个用于两个 Int16 列的复合键,这些列在 oracle 11g 数据库上映射为数字。

[Serializable]
public class AgenciaPK
{
    [DisplayName("Código")]
    public virtual System.Int16 CdAgencia { get; set; }
    public virtual System.Int16 CdOrgaoArrecad { get; set; }

    public override int GetHashCode()
    {
        int hashCode = 0;
        hashCode = hashCode ^ CdAgencia.GetHashCode() ^ CdOrgaoArrecad.GetHashCode();
        return hashCode;
    }
    public override bool Equals(object obj)
    {
        var toCompare = obj as Agencia;
        if (toCompare == null)
        {
            return false;
        }
        return (this.GetHashCode() != toCompare.GetHashCode());
    } 
}

public class Agencia : AgenciaPK
{        
    public virtual OrgaoArrecad OrgaoArrecad { get; set; }
    [DisplayName("Nome")]
    public virtual string NmAgencia { get; set; }
    [DisplayName("Digito")]
    public virtual string NrDigitoAgencia { get; set; }
    [DisplayName("Número")]
    public virtual string NrAgencia { get; set; }
    [DisplayName("Descrição")]
    [StringLength(20)]
    public virtual string DsCompl { get; set; }
    [DisplayName("Logradouro")]
    public virtual string NmLograd { get; set; }
    [DisplayName("Bairro")]
    public virtual string NmBairro { get; set; }
    public virtual System.Nullable<short> IdSituacAgencia { get; set; }
    [DisplayName("Cep")]
    public virtual System.Nullable<short> NrCep { get; set; }
}

我有另一个名为 Convenio 的类,它有一个由一个字符串列 (CdConvenio) 组成的复合键,另一个是 int CdOrgaoArrecad。

 [Serializable]
public class ConvenioPK
{
    [DisplayName("Código")]
    [StringLength(20)]
    public virtual string CdConvenio { get; set; }

    [DisplayName("Cód. Órgão arrecadador")]
    public virtual System.Int16 CdOrgaoArrecad { get; set; }
    public override int GetHashCode()
    {
        int hashCode = 13;
        if (CdConvenio != null)
        hashCode = hashCode ^ CdConvenio.GetHashCode() ^ CdOrgaoArrecad.GetHashCode();
        return hashCode;
    }
    public override bool Equals(object obj)
    {
        var toCompare = obj as Convenio;
        if (toCompare == null)
        {
            return false;
        }
        return (this.GetHashCode() != toCompare.GetHashCode());
    } 
}

public class Convenio : ConvenioPK
{
    [DisplayName("Órgão arrecadador")]
    public virtual OrgaoArrecad OrgaoArrecad { get; set; }
    [DisplayName("Data início")]
    [DataType(DataType.Date)]
    public virtual System.DateTime DtInicioConvenio { get; set; }
    [DisplayName("Data término")]
    [DataType(DataType.Date)]
    public virtual System.DateTime DtFinalConvenio { get; set; }
    [DisplayName("Tipo")]
    public virtual short TpConvenio { get; set; }
    [DisplayName("Tarifa")]
    public virtual short TpTarifaConvenio { get; set; }
    [DisplayName("Valor tarifa")]
    public virtual Double VlTarifaConvenio { get; set; }
    [DisplayName("Quantidade dias")]
    public virtual short QtDiasFloat { get; set; }
}

当我使用这个映射类 AgenciaMap 访问这个类 Agencia 时,它工作正常。

public class AgenciaMap : FluentNHibernate.Mapping.ClassMap<Agencia>
{        
    public AgenciaMap() {
        Table("SCI_AGENCIA");
        CompositeId()
            .KeyProperty(x => x.CdAgencia, "CD_AGENCIA")
            .KeyProperty(x => x.CdOrgaoArrecad, "CD_ORGAO_ARRECAD");
        Map(x => x.CdOrgaoArrecad).Column("CD_ORGAO_ARRECAD").Not.Nullable();
        Map(x => x.NmAgencia).Column("NM_AGENCIA").CustomType("AnsiString").Length(30).Not.Nullable();

        References<OrgaoArrecad>(x => x.OrgaoArrecad, "CD_ORGAO_ARRECAD").ForeignKey().Not.Update()
          .Not.Insert()
          .Cascade.None().Not.Nullable().Not.LazyLoad().Fetch.Join();
    }
}

但是,当我尝试使用 ConvenioMap 类访问 Convenio 类时:

 public class ConvenioMap : FluentNHibernate.Mapping.ClassMap<Convenio>
{        
    public ConvenioMap() {
        Table("CONVENIO");
        CompositeId()
           .KeyProperty(x => x.CdConvenio, "CD_CONVENIO")
           .KeyProperty(x => x.CdOrgaoArrecad, "CD_ORGAO_ARRECAD");

        Map(x => x.CdConvenio).Column("CD_CONVENIO").CustomSqlType("varchar2(20)").CustomType("AnsiString").Length(20).Not.Nullable();            
        Map(x => x.CdOrgaoArrecad).Column("CD_ORGAO_ARRECAD").Not.Nullable();
        Map(x => x.DtInicioConvenio).Column("DT_INICIO_CONVENIO").CustomSqlType("DATE").Not.Nullable();
        Map(x => x.DtFinalConvenio).Column("DT_FINAL_CONVENIO").CustomSqlType("DATE").Not.Nullable();
        Map(x => x.TpConvenio).Column("TP_CONVENIO").CustomSqlType("NUMBER(2,0)").Not.Nullable();
        Map(x => x.TpTarifaConvenio).Column("TP_TARIFA_CONVENIO").CustomSqlType("NUMBER(2,0)").Not.Nullable();            
        Map(x => x.VlTarifaConvenio).Column("VL_TARIFA_CONVENIO").CustomSqlType("NUMBER(15,2)").Not.Nullable();  
        Map(x => x.QtDiasFloat).Column("QT_DIAS_FLOAT").CustomSqlType("NUMBER(3,0)").Not.Nullable();

        References(x => x.OrgaoArrecad)
            .Column("CD_ORGAO_ARRECAD")
            .ForeignKey().Not.Update()
            .Not.Insert()
            .Cascade.None()
            .Not.Nullable()
            .Not.LazyLoad()
            .Fetch.Join();
    }
}

当我映射类型字符串“CD_CONVENIO”的列时,我收到此错误:

SCI.CONVENIO 中列 CD_CONVENIO 的列类型错误。找到:varchar2,预期为 NVARCHAR2(255)

我有疑问的代码的具体部分是:

CompositeId()
           .KeyProperty(x => x.CdConvenio, "CD_CONVENIO")
           .KeyProperty(x => x.CdOrgaoArrecad, "CD_ORGAO_ARRECAD");

所以,这是我的问题:我做错了什么?

4

0 回答 0