简体   繁体   中英

Error when mapping composite tables using Fluent NHibernate

I'm having a problem while mapping a compound table using fluent nhibernate, it follows the code and the problem returned so that somebody let me assist in this problem:

AmostraMap.cs:

using FluentNHibernate.Automapping;
using FluentNHibernate.Mapping;
using PedidoWeb.Dominio;

namespace PedidoWeb.Persistencia
{
    public class AmostraMap : ClassMap<Amostra>
    {
        public AmostraMap()
        {
            Table("USU_V200CAP");

            Id(u => u.Codigo, "NUMAMO");//.GeneratedBy.Assigned();

            Map(g => g.DataAmostra, "DATAMO");
            Map(g => g.Propriedade.Codigo, "SEQPRO");
            Map(g => g.QuantidadeSacas, "QTDSAC");
            Map(g => g.Situacao, "SITAMO");
            Map(g => g.NomeContato, "NOMCTO");
            Map(g => g.TelefoneContato, "TELCTO");
            Map(g => g.DataGeracao, "DATGER");
            Map(g => g.HoraGeracao, "HORGER");
            Map(g => g.TipoAnalise, "TIPANA");
            Map(g => g.AnalisePontuacao, "ANAPON");

            //References(u => u.Empresa, "CODEMP").LazyLoad();
            //References(u => u.Cooperado, "MATCPR");
            //References(u => u.Fornecedor, "CODFOR");
            //References(u => u.Usuario, "USUGER");
            //References(u => u.Filial, "CODFIL");
            //References(u => u.Representante, "CODREP").LazyLoad();            
            HasMany<Talhao>(u => u.Talhao).KeyColumns.Add("CODTAL", "CODEMP", "MATCPR", "SEQPRO").Cascade.All();
        }

        //public void Override(AutoMapping<Amostra> mapping)
        //{
        //    mapping.Id(x => x.Id).Column("RecordId");
        //    mapping.Map(x => x.Name).Not.Nullable();
        //    mapping.References(x => x.Parent).Not.Nullable().Column("ParentRecordId");
        //    mapping.References(x => x.Type).Not.Nullable().Column("TypeId");
        //}
    }
}

TalhaoMap.cs:

using FluentNHibernate.Mapping;
using PedidoWeb.Dominio;

namespace Sapiens.PedidoWeb.Persistencia.Mapeamento
{
    class TalhaoMap:ClassMap<Talhao>
    {
        TalhaoMap()
        {
            Table("usu_t113tal");
            //Id(e => e.Codigo, "USU_CODTAL");

            CompositeId()
                .KeyProperty(x => x.Codigo, "USU_CODTAL")
                .KeyReference(x => x.Empresa, "USU_CODEMP")
                .KeyReference(x => x.Cooperado, "USU_MATCPR")
                .KeyReference(x => x.Propriedade, "USU_SEQPRO", "USU_CODEMP", "USU_MATCPR");
            References(e => e.Empresa, "USU_CODEMP");
            References(e => e.Cooperado, "USU_MATCPR");
            //HasMany<Propriedade>(x => x.Propriedade).KeyColumns.Add("USU_SEQPRO", "USU_CODEMP", "USU_MATCPR").Cascade.All();

            Map(e=>e.AreaTotal, "USU_ARETOT");
            Map(e=>e.Latitude, "USU_LATTAL");
            Map(e=>e.Logitude, "USU_LONTAL");
            Map(e=>e.Altitude, "USU_ALTTAL");
            Map(e=>e.EspacoPlantas, "USU_ESPPLA");
            Map(e=>e.EspacoCarreiras, "USU_ESPCAR");
            Map(e=>e.QuantidadeCovas, "USU_QTDCOV");
            Map(e=>e.CodigoMeeiro, "USU_CODMEE");
            Map(e=>e.VencimentoMeeiro, "USU_VCTMEE");
            Map(e=>e.BloqMeeiro, "USU_BLOMEE");
            Map(e=>e.CodigoMotivoBloqueio, "USU_CODMBL");
            Map(e=>e.DataBloqueio, "USU_DATBLO");
            Map(e=>e.NumeroAnos, "USU_NROANO");
            Map(e=>e.DataInicio, "USU_DATINI");
            Map(e=>e.PercentagemMeeiro, "USU_PERMEE");
            Map(e=>e.PercentagemPropietario, "USU_PERPRO");
            Map(e=>e.RespDespesas, "USU_RESDSP");
            Map(e=>e.CodigoEspecieTipoProduto, "USU_CODETP");
            Map(e=>e.CodigoCultivar, "USU_CODCUL");
            Map(e=>e.AnoPlantio, "USU_ANOPLA");
            Map(e=>e.FaceLavoura, "USU_FACLAV");
            Map(e=>e.SituacaoTalhao, "USU_SITTAL");
            Map(e=>e.NomeTalhao, "USU_NOMTAL");
            Map(e=>e.Escriturado, "USU_TALESC");

        }
    }
}

Exception:

Foreign key (FKEA30818A6C05BC04:usu_t113tal [CODTAL, CODEMP, MATCPR, SEQPRO])) must have same number of columns as the referenced primary key (USU_V200CAP [NUMAMO])

As the error description says:

... must have same number of columns as the referenced primary key...

The problem you are facing is that TalhaoMap has USU_CODEMP, USU_MATCPR fields in the composite ID definition, while AmostraMap references it without these two fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM