Fluent nhibernate 一对多映射

Fluent nhibernate 一对多映射,fluent-nhibernate,Fluent Nhibernate,您好,我在使用fluent nhibernate进行映射时遇到问题: public ProductMap() { Id(x => x.ProductID); Map(x => x.ProductName); Map(x => x.QuantityPerUnit); Map(x => x.ReorderLevel); Map(x => x.SupplierID);

您好,我在使用fluent nhibernate进行映射时遇到问题:

     public ProductMap()
    {
        Id(x => x.ProductID);
        Map(x => x.ProductName);
        Map(x => x.QuantityPerUnit);
        Map(x => x.ReorderLevel);
        Map(x => x.SupplierID);
        Map(x => x.UnitPrice);
        Map(x => x.UnitsInStock);
        Map(x => x.UnitsOnOrder);
        Map(x => x.CategoryID);
        Map(x => x.Discontinued);
        References(x => x.Category).Column("CategoryID");
        References(x => x.Supplier).Column("SupplierID");
        Table("dbo.Products");
    }

    public SupplierMap()
    {
        Id(x => x.SupplierID);
        Map(x => x.Address);
        Map(x => x.City);
        Map(x => x.CompanyName);
        Map(x => x.ContactName);
        Map(x => x.ContactTitle);
        Map(x => x.Country);
        Map(x => x.Fax);
        Map(x => x.HomePage);
        Map(x => x.Phone);
        Map(x => x.PostalCode);
        Map(x => x.Region);
        HasMany(x => x.Products)
            .KeyColumn("SupplierID")
                       .Inverse()
                      .Cascade.All();
        Table("dbo.Suppliers");
    }

     public CategoryMap()
    {
        Id(x => x.CategoryID);
        Map(x => x.CategoryName);
        Map(x => x.Description);
        Map(x => x.Picture);
        HasMany(x => x.Products)
            .KeyColumn("CategoryID")
                     .Inverse()
                    .Cascade.All();
        Table("dbo.Categories");
    }
当我尝试这样做时:

    var sessionFactory = SessionFactory.CreateSessionFactory();
        using (var session = sessionFactory.OpenSession())
        {
            Product fresh = new Product()
                                {
                                    ProductName = "AddFromNhibernate",
                                    SupplierID = 3,
                                    CategoryID = 5,
                                    QuantityPerUnit = "1kg pcg",
                                    UnitPrice = (decimal) 35,
                                     UnitsInStock = (short?) 15,
                                    UnitsOnOrder = 0,
                                    ReorderLevel = null,
                                    Discontinued = true
                                };
            using (session.BeginTransaction())
            {

                session.Save(fresh);
                session.Transaction.Commit();
            }
        }
我得到了系统索引。
我的映射有什么问题?当我尝试获取一些数据时,工作正常,但添加失败。我尝试在ProductMap中引用specyfie Propertyreference,但没有任何帮助。

我注意到您将一些列映射了两次

ProductMap
为例:

public ProductMap()
{
    (...)
    Map(x => x.SupplierID);
    (...)
    Map(x => x.CategoryID);
    References(x => x.Category).Column("CategoryID");
    References(x => x.Supplier).Column("SupplierID");
    (...)
}
在这种情况下,如果希望保留原始ID和引用(因为它们映射到数据库表中的同一列),则应绕过其中一个中的插入/更新

我的意思是:

public ProductMap()
{
    (...)
    Map(x => x.SupplierID).Not.Insert().Not.Update();
    (...)
    Map(x => x.CategoryID).Not.Insert().Not.Update();
    References(x => x.Category).Column("CategoryID");
    References(x => x.Supplier).Column("SupplierID");
    (...)
}

如果不这样做,当NHibernate开始设置插入/更新参数时,您可能会得到您描述的
异常。

thx,它现在可以工作了。但我有一个问题:当我这样做的时候,我必须为产品创建类别和供应商对象,不能简单地根据代码设置它们的id。有没有可能是双向的?我设置了CategoryId和SupplierID还是设置了CategoryId和SupplierID?我想你的疑问适合另一个帖子(问题):)