c# - 流利的 Nhibernate,挣扎于一对多关系

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

下面的异常总是被抛出:

could not initialize a collection: [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 as Banco3_9_0_, ofertas0_1_.Culto_Id as Culto4_9_0_, ofertas0_1_.Frequentador_Id as Frequent5_9_0_, ofertas0_1_.NomeAvulso as NomeAvulso9_0_, ofertas0_1_.NumeroCheque as NumeroCh7_9_0_, ofertas0_1_.Valor as Valor9_0_, ofertas0_.TipoOferta_Id as TipoOferta2_11_0_ FROM dbo.[Oferta] ofertas0_ inner join dbo.[Contribuicao] ofertas0_1_ on ofertas0_.Id=ofertas0_1_.Id WHERE ofertas0_.Culto_Id=?]

它的内部异常是:

Invalid column name 'Culto_Id'. Invalid column name '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 扩展而来。

最佳答案

请尝试在您的代码中进行以下更改:

Table("`Contribuicao`"); 

对于 Table("Contribuicao");

并在所有 Table 定义中添加它。

关于c# - 流利的 Nhibernate,挣扎于一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7044030/

相关文章:

php - 使用连接库时如何在 codeigniter 上创建别名

c# - 任何人都可以解释位图数据中步幅的功能吗?

mysql - 具有挑战性的 SQL 查询来执行矩阵乘法

c# - 运行动画后如何导航到另一个页面

MySQL子查询或连接不同数据库,其中数据库名称在主查询的结果集中

nhibernate - Fluent Nhibernate 复合键映射

mysql - (Fluent) NHibernate composite-id 问题 : MySQL complains that the parameter index is out of bounds

c# - NHibernate 错误 - "could not initialize a collection"

c# - 如何制作 PInvoke 友好的 native API?

c# - 如何从异步方法向调用者抛出异常?