强制fluent-nhibernate加入正确的列?

时间:2014-12-19 15:25:51

标签: c# nhibernate fluent-nhibernate db2-400

使用这些实体时:

[AS400Entity]
public class AuthItemEntity
{
    public virtual decimal Upc { get; set; }
    public virtual string VendorItemCode { get; set; }
    public virtual int Vendor { get; set; }
    public virtual int ProductVendorNumber { get; set; }
    public virtual string Description { get; set; }
    public virtual DateTime AddDate { get; set; }
    public virtual DateTime VendorDiscontinueDate { get; set; }

    public virtual VendorMasterEntity VendorMaster { get; set; }
}

[AS400Entity]
public class VendorMasterEntity
{
    public virtual int CompanyNumber { get; set; }
    public virtual int Vendor { get; set; }
    public virtual CompNamesEntity CompNames { get; set; }
    public virtual IList<AuthItemEntity> AuthItems { get; set; }
}

[AS400Entity]
public class CompNamesEntity
{
    public virtual int CompanyNumber { get; set; }
    public virtual string CompanyName { get; set; }
    public virtual IList<VendorMasterEntity> VendorMasts { get; set; }
}

这个映射:

public class AuthItemEntityMapping : ClassMap<AuthItemEntity>
{
    public AuthItemEntityMapping()
    {
        Table("AUTHITEM");
        Id(entity => entity.Vendor, "ITEM_VENDOR");
        Map(entity => entity.ProductVendorNumber, "PRODUCT_VENDOR");
        Map(model => model.Upc, "ITEM_UPC");
        Map(model => model.VendorItemCode, "VENDOR_ITEM_CODE");
        Map(model => model.Vendor, "ITEM_VENDOR");
        Map(model => model.Description, "ITEM_DESCRIPTION");
        Map(model => model.AddDate, "ADD_DATE").CustomType("Date");
        Map(model => model.VendorDiscontinueDate, "VENDOR_DISCONTINUE_DATE").CustomType("Date");

        HasOne(entity => entity.VendorMaster).Fetch.Join().ForeignKey("ITEM_VENDOR");
    }
}

public class VendorMasterEntityMap : ClassMap<VendorMasterEntity>
{
    public VendorMasterEntityMap()
    {
        Table("VENDMAST");

        Id(entity => entity.Vendor, "ITEM_VENDOR");
        Id(entity => entity.CompanyNumber, "COMPANY_NUMBER");

        HasOne(entity => entity.CompNames).Fetch.Join().ForeignKey("COMPANY_NUMBER");

        HasMany(entity => entity.AuthItems).Cascade.All();
    }
}

public class CompNamesEntityMap : ClassMap<CompNamesEntity>
{
    public CompNamesEntityMap()
    {
        Table("COMPNAMES");
        Id(entity => entity.CompanyNumber, "COMPANY_NUMBER");
        Map(entity => entity.CompanyName, "COMPANY_NAME");

        HasMany(entity => entity.VendorMasts).Cascade.All();
    }
}

我得到这个SQL:

SELECT this_.ITEM_VENDOR as ITEM1_3_2_, this_.PRODUCT_VENDOR as PRODUCT2_3_2_, this_.ITEM_UPC as ITEM3_3_2_, this_.VENDOR_ITEM_CODE as VENDOR4_3_2_, this_.ITEM_DESCRIPTION as ITEM5_3_2_, this_.ADD_DATE as ADD6_3_2_, this_.VENDOR_DISCONTINUE_DATE as VENDOR7_3_2_, vendormast2_.COMPANY_NUMBER as COMPANY1_5_0_, compnamese3_.COMPANY_NUMBER as COMPANY1_4_1_, compnamese3_.COMPANY_NAME as COMPANY2_4_1_ 
FROM AUTHITEM this_ 
    left outer join VENDMAST vendormast2_ on this_.ITEM_VENDOR=vendormast2_.COMPANY_NUMBER <-- should be item_vendor
    left outer join COMPNAMES compnamese3_ on vendormast2_.COMPANY_NUMBER=compnamese3_.COMPANY_NUMBER WHERE this_.ITEM_UPC = ? and this_.VENDOR_DISCONTINUE_DATE = ?

这只是一个远离正确的财产参考。

请注意:我到处都在使用HasOne(),因为我无法进行延迟加载,必须在查询时加载。

1 个答案:

答案 0 :(得分:0)

截至2014年12月,您仍然无法解决此问题:

https://github.com/jagregory/fluent-nhibernate/issues/272

它有意义因为它是一个ORM而我们试图在没有正确强制关系的情况下在数据库上使用它(密钥丢失但数据的行为方式如此)。

这个问题迫使我的公司放弃了NHibernate。

我希望这个答案可以避免别人犯同样的错误并浪费时间。