Fluent NHibernate - 新版本中的 HasMany 错误

发布于 2025-01-06 14:56:06 字数 2571 浏览 2 评论 0原文

我将 NHibernate 升级为 3.1.0.4000,将 Fluent 升级为 1.2.0.712,并且 HasMany 存在一些问题...

我的实体:

public class MateriaPrima
{
        public virtual int Id { get; set; }
        public virtual string Description { get; set; }
        public virtual DateTime Date { get; set; }
        public virtual decimal Price { get; set; }
}

public class Product
    {
        public virtual int Id { get; set; }
        public virtual IList<ProductMateriaPrima> ListMateriaPrima { get; set; }
        public virtual string Description { get; set; }
        public virtual decimal Price { get; set; }
        public virtual DateTime Date { get; set; }

        public Product()
        {
            this.ListaMateriaPrima = new List<ProductMateriaPrima>();
        } 
}

public class ProductMateriaPrima
{
    public virtual int Id { get; set; }
    public virtual Product Product {get;set;}
    public virtual MateriaPrima MateriaPrima { get; set; }
    public virtual decimal PrecoCusto {get;set;}
}

和地图:

public class MateriaPrimaMap : ClassMap<MateriaPrima>
{
    public MateriaPrimaMap()
    {
        Id(m => m.Id).Length(11).Not.Nullable();
        Map(m => m.Description).Length(90).Not.Nullable();
        Map(m => m.Date).Not.Nullable();
        Map(m => m.Price).Not.Nullable();
    }
}

public class ProductMateriaPrimaMap : ClassMap<ProductoMateriaPrima>
    {
        public ProductMateriaPrimaMap()
        {
            Id(c => c.Id).Length(11);
            Map(c => c.Price).Not.Nullable();

            References(c => c.MateriaPrima).Column("IdMateriaPrima").Not.LazyLoad();
            References(c => c.Product).Column("IdProduct").Not.LazyLoad();
        }
    }

public class ProdutoMap : ClassMap<Produto>
    {
        public ProdutoMap()
        {
            Id(m => m.Id).Length(11).Not.Nullable();
            Map(m => m.Description).Length(90).Not.Nullable();
            Map(m => m.Price).Length(10);
            Map(m => m.Date).Length(12);
            Map(m => m.Active).Not.Nullable();

            HasMany(x => x.ListaMateriaPrima)
                    .Table("ProdutoMateriaPrima")
                    .KeyColumn("IdProduto")
                    .KeyColumn("IdMateriaPrima")
                    .Inverse()
                    .Cascade.AllDeleteOrphan();
        }
    }

当我尝试搜索时,我收到错误:{“未知列 'listamater0_.MateriaPrima_id' in 'field list'"}

当我使用旧版本的 Nhibernate 和 Fluent 时,不会发生此错误... 有人知道发生了什么事吗? 感谢您的帮助...

I Upgrade my NHibernate for 3.1.0.4000 and Fluent for 1.2.0.712 and have some problems with HasMany...

my entities:

public class MateriaPrima
{
        public virtual int Id { get; set; }
        public virtual string Description { get; set; }
        public virtual DateTime Date { get; set; }
        public virtual decimal Price { get; set; }
}

public class Product
    {
        public virtual int Id { get; set; }
        public virtual IList<ProductMateriaPrima> ListMateriaPrima { get; set; }
        public virtual string Description { get; set; }
        public virtual decimal Price { get; set; }
        public virtual DateTime Date { get; set; }

        public Product()
        {
            this.ListaMateriaPrima = new List<ProductMateriaPrima>();
        } 
}

public class ProductMateriaPrima
{
    public virtual int Id { get; set; }
    public virtual Product Product {get;set;}
    public virtual MateriaPrima MateriaPrima { get; set; }
    public virtual decimal PrecoCusto {get;set;}
}

And Maps:

public class MateriaPrimaMap : ClassMap<MateriaPrima>
{
    public MateriaPrimaMap()
    {
        Id(m => m.Id).Length(11).Not.Nullable();
        Map(m => m.Description).Length(90).Not.Nullable();
        Map(m => m.Date).Not.Nullable();
        Map(m => m.Price).Not.Nullable();
    }
}

public class ProductMateriaPrimaMap : ClassMap<ProductoMateriaPrima>
    {
        public ProductMateriaPrimaMap()
        {
            Id(c => c.Id).Length(11);
            Map(c => c.Price).Not.Nullable();

            References(c => c.MateriaPrima).Column("IdMateriaPrima").Not.LazyLoad();
            References(c => c.Product).Column("IdProduct").Not.LazyLoad();
        }
    }

public class ProdutoMap : ClassMap<Produto>
    {
        public ProdutoMap()
        {
            Id(m => m.Id).Length(11).Not.Nullable();
            Map(m => m.Description).Length(90).Not.Nullable();
            Map(m => m.Price).Length(10);
            Map(m => m.Date).Length(12);
            Map(m => m.Active).Not.Nullable();

            HasMany(x => x.ListaMateriaPrima)
                    .Table("ProdutoMateriaPrima")
                    .KeyColumn("IdProduto")
                    .KeyColumn("IdMateriaPrima")
                    .Inverse()
                    .Cascade.AllDeleteOrphan();
        }
    }

When i try to search, i got the error: {"Unknown column 'listamater0_.MateriaPrima_id' in 'field list'"}

This error don´t happen when i had the old version of Nhibernate and Fluent...
Someone know what´s happening?
Thanks for the help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘑菇王子 2025-01-13 14:56:06

发生此错误是因为您没有设置 Id 属性的列名称,id 的默认模式是:entityname_id。
现在在 ProductMateriaPrimaMap 类中,您写道:

References(c => c.MateriaPrima).Column("IdMateriaPrima")

这意味着在您的 MateriaPrimaMap 类中,您应该有列名称为 IdMateriaPrima 的 Id,但默认名称为:MateriaPrima_Id。

我无法确切地说问题出在哪里,因为您没有显示查询。但我知道当 Id 列名称与参考列名称不同时会发生此错误。

This error happend because you didn't set collumn name of Id property, default pattern for id is: entityname_id.
And now in ProductMateriaPrimaMap class you wrote:

References(c => c.MateriaPrima).Column("IdMateriaPrima")

This mean that in you MateriaPrimaMap class you should have Id with collumn name: IdMateriaPrima, but default you have name: MateriaPrima_Id.

I can't say where the problem is exactly because you didn't show the query. But i know that this error happend when Id column name is diffrent from Reference column name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文