流畅的NHibernate错误:必须与引用的主键

时间:2016-12-07 12:08:39

标签: nhibernate fluent-nhibernate foreign-key-relationship fluent-nhibernate-mapping

我是Fluent NHibernate的新手,我对特定HasMany关系有疑问。

我有三个对象:UserCompanyCompanyAddress

User正确引用Company,而Company会引用CompanyAddress(但这会失败)。

Company映射的组成如下:

public class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Schema("company");
        Table("LIST");

        this.CompositeId(x => x.key)
            .KeyProperty(x => x.applicationId, "APPLICATION_ID")
            .KeyProperty(x => x.id, "ID")
            .KeyProperty(x => x.userId, "USER_ID");

        this.Map(x => x.vat, "VAT");

        this.HasMany(x => x.addresses)
            .Not.LazyLoad()
            .Cascade.None();

        this.References(x => x.user)
            .Class<User>()
            .Columns("APPLICATION_ID", "USER_ID")
            .Insert()
            .Update()
            .Fetch.Select()
            .Not.LazyLoad()
            .Cascade.None();
    }
}

CompanyAddress是:

public class CompanyAddressMap : ClassMap<CompanyAddress>
{
    public CompanyAddressMap()
    {
        Schema("[address]");
        Table("COMPANY_LIST");

        this.CompositeId(x => x.key)
            .KeyProperty(x => x.applicationId, "APPLICATION_ID")
            .KeyProperty(x => x.id, "ID")
            .KeyProperty(x => x.entityId, "COMPANY_ID");

        this.Map(x => x.country, "COUNTRY");
        this.Map(x => x.province, "PROVINCE");
        this.Map(x => x.region, "REGION");
        this.Map(x => x.city, "CITY");
        this.Map(x => x.address, "[ADDRESS]");
        this.Map(x => x.description, "DESCRIPTION");

        this.References(x => x.company)
            .Class<Company>()
            .Columns("APPLICATION_ID", "COMPANY_ID")
            .Insert()
            .Update()
            .Fetch.Select()
            .Not.LazyLoad()
            .Cascade.None();
    }
}

但是我收到了这个错误:

  

外键(FK8DE1BC098F20893F:COMPANY_LIST [APPLICATION_ID,COM​​PANY_ID]))必须与引用的主键具有相同的列数(LIST [APPLICATION_ID,ID,USER_ID])

我需要使用非主键引用AddressCompany,因为Company已引用User,因此我的问题是:

如何在不修改数据库中定义的真实外键的情况下使用Fluent NHibernate引用此外键?

[编辑]

  

我需要使用非主键引用AddressCompany'因为User已经引用了公司,所以我的问题是:如何使用流畅的nhibernate引用此外键而不修改定义到数据库中的真实外键?

我不想将USER_ID字段添加到[address]。[COMPANY_LIST]表中,我不想将userId字段添加到AddressCompany类...'原因是无用字段...关系是:

user.LIST
applicationId(pk)
id(pk)

company.LIST
applicationId(pk)(fk - &gt; user.LIST(applicationId))
id(pk)
userId(pk)(fk - &gt; user.LIST(id))

address.COMPANY_LIST
applicationId(pk)(fk - &gt; company.LIST(applicationId))
id(pk)
companyId(pk)(fk - &gt; company.LIST(id))

nhibernate为什么要强迫我只引用确切的主键? 我可以引用公司类主键的子集吗?

非常感谢...

1 个答案:

答案 0 :(得分:0)

从您的映射和错误消息中可以看出错误

  

外键(FK8DE1BC098F20893F:COMPANY_LIST [APPLICATION_ID,   COMPANY_ID]))必须具有与引用的列数相同的列数   主键(LIST [APPLICATION_ID,ID,USER_ID])

您对CompanyMap的映射将主键映射为三列

    this.CompositeId(x => x.key)
        .KeyProperty(x => x.applicationId, "APPLICATION_ID")
        .KeyProperty(x => x.id, "ID")
        .KeyProperty(x => x.userId, "USER_ID");

但您在CompanyAddressMap中对公司的引用仅映射回具有2列的公司

    this.References(x => x.company)
        .Class<Company>()
        .Columns("APPLICATION_ID", "COMPANY_ID")
        .Insert()
        .Update()
        .Fetch.Select()
        .Not.LazyLoad()
        .Cascade.None();

将ID列添加到列列表中。