流利的Nhibernate,与一对多的关系挣扎

时间:2011-08-12 17:31:10

标签: c# join fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping

始终抛出以下异常:

  

无法初始化集合:[FatorM.Sieq.Model.Entities.Culto.Ofertas#4] [SQL:SELECT ofertas0_.Culto_Id as Culto3_1_,ofertas0_.Id as Id1_,ofertas0_.Id as Id9_0_,ofertas0_1_.Anonimo as Anonimo9_0_,ofertas0_1_.Banco_Id如Banco3_9_0_,ofertas0_1_.Culto_Id如Culto4_9_0_,ofertas0_1_.Frequentador_Id如Frequent5_9_0_,ofertas0_1_.NomeAvulso如NomeAvulso9_0_,ofertas0_1_.NumeroCheque如NumeroCh7_9_0_,ofertas0_1_.Valor如Valor9_0_,ofertas0_.TipoOferta_Id如TipoOferta2_11_0_ FROM DBO。[Oferta] ofertas0_内部联接dbo。[Contribuicao] ofertas0_1_ on ofertas0_.Id = ofertas0_1_.Id WHERE ofertas0_.Culto_Id =?]

内在的例外是:

  

无效的列名'Culto_Id'。列名称'Culto_Id'无效。

是的!两次...

这里有代码:

1)CultoMapping.cs:

using System;
using FluentNHibernate.Mapping;

namespace FatorM.Sieq.Model.Entities
{
    public class CultoMapping : ClassMap<Culto>
    {
        public CultoMapping()
        {
            Table("`Culto`");
            Schema("dbo");
            Id(x => x.Id)
                .GeneratedBy.Identity();
            OptimisticLock.Version();
            Not.LazyLoad();
            Map(x => x.DataCulto);
            Map(x => x.Frequentador_Id_Tesoureiro, "Frequentador_Id_Tesoureiro")
                .Not.Insert()
                .Not.Update();
            Map(x => x.Igreja_Id, "Igreja_Id")
                .Not.Insert()
                .Not.Update();
            Map(x => x.NumeroBatizados);
            Map(x => x.NumeroCulto);
            Map(x => x.NumeroPresentes);
            Map(x => x.NumeroVisitantes);
            Map(x => x.Pastor_Id, "Pastor_Id")
                .Not.Insert()
                .Not.Update();
            Map(x => x.TotalCongregacoes);
            Map(x => x.TotalDizimos);
            Map(x => x.TotalMissoes);
            Map(x => x.TotalOfertasEspeciais);
            Map(x => x.TotalOfertasGeral);
            Map(x => x.TotalOutrasEntradas);
            HasMany(x => x.Dizimos)
                .KeyColumn("Culto_Id")
                .Inverse()
                .Cascade.All()
                .Fetch.Select().Not.LazyLoad()
                .AsBag();
            HasMany(x => x.Ofertas)
                .KeyColumn("Culto_Id")
                .Inverse()
                .Cascade.All()
                .Fetch.Select().Not.LazyLoad()
                .AsBag();
            HasManyToMany(x => x.Diaconos)
                .ChildKeyColumn("Frequentador_Id")
                .ParentKeyColumn("Culto_Id")
                .Cascade.All()
                .Table("CultoDiacono")
                .Fetch.Select().Not.LazyLoad()
                .AsBag();
            References(x => x.Tesoureiro)
                .Class(typeof(Frequentador))
                .Not.Nullable() 
                .Column("Frequentador_Id_Tesoureiro")
                .Fetch.Select().Not.LazyLoad()
                .Cascade.SaveUpdate();
            References(x => x.Igreja)
                .Class(typeof(Igreja))
                .Not.Nullable() 
                .Column("Igreja_Id")
                .Fetch.Select().Not.LazyLoad()
                .Cascade.SaveUpdate();
            References(x => x.Pastor)
                .Class(typeof(Pastor))
                .Not.Nullable() 
                .Column("Pastor_Id")
                .Fetch.Select().Not.LazyLoad()
                .Cascade.SaveUpdate();
        }
    }
}

2)OfertaMapping.cs:

    using System;
    using FluentNHibernate.Mapping;

    namespace FatorM.Sieq.Model.Entities
    {
        public class OfertaMapping : SubclassMap<Oferta>
        {
            public OfertaMapping()
            {
                Table("`Oferta`");
                Schema("dbo");
                KeyColumn("Id");
                Not.LazyLoad();
                Map(x => x.TipoOferta_Id, "TipoOferta_Id")
                    .Not.Insert()
                    .Not.Update();
                References(x => x.TipoOferta)
                    .Class(typeof(TipoOferta))
                    .Not.Nullable()
                    .Column("TipoOferta_Id")
                    .Fetch.Select().Not.LazyLoad()
                    .Cascade.All();
            }
        }
    }

3)DizimoMapping.cs:

using System;
using FluentNHibernate.Mapping;

namespace FatorM.Sieq.Model.Entities
{
    public class DizimoMapping : SubclassMap<Dizimo>
    {
        public DizimoMapping()
        {
            Table("`Dizimo`");
            Schema("dbo");
            KeyColumn("Id");
            Not.LazyLoad();
            Map(x => x.AnoReferencia);
            Map(x => x.MesReferencia);
        }
    }
}

4)ContribuicaoMapping.cs:

using System;
using FluentNHibernate.Mapping;

namespace FatorM.Sieq.Model.Entities
{
    public class ContribuicaoMapping : ClassMap<Contribuicao>
    {
        public ContribuicaoMapping()
        {
            Table("`Contribuicao`");
            Schema("dbo");
            Id(x => x.Id)
                .GeneratedBy.Identity();
            OptimisticLock.Version();
            Not.LazyLoad();
            Map(x => x.Anonimo);
            Map(x => x.Banco_Id, "Banco_Id")
                .Not.Insert()
                .Not.Update();
            Map(x => x.Culto_Id, "Culto_Id")
                .Not.Insert()
                .Not.Update();
            Map(x => x.Frequentador_Id, "Frequentador_Id")
                .Not.Insert()
                .Not.Update();
            Map(x => x.NomeAvulso);
            Map(x => x.NumeroCheque);
            Map(x => x.Valor);
            References(x => x.Banco)
                .Class(typeof(Banco))   
                .Column("Banco_Id")
                .Fetch.Select().Not.LazyLoad()
                .Cascade.SaveUpdate();
            References(x => x.Culto)
                .Class(typeof(Culto))
                .Not.Nullable() 
                .Column("Culto_Id")
                .Fetch.Select().Not.LazyLoad()
                .Cascade.SaveUpdate();
            References(x => x.Frequentador)
                .Class(typeof(Frequentador))    
                .Column("Frequentador_Id")
                .Fetch.Select().Not.LazyLoad()
                .Cascade.SaveUpdate();
        }
    }
}

Oferta和Dizimo从Contribuicao延伸。

1 个答案:

答案 0 :(得分:1)

请在代码中尝试以下更改:

Table("`Contribuicao`"); 

代表Table("Contribuicao");

并在所有Table定义中进行此操作。