两个表的 Fluent Nhibernate 映射一对多在映射中使用唯一而不是主键

FluentNhibernate mapping of two tables oneToMany using unique instea of primary key in mapping

我正在用 C# 编写桌面项目。我正在使用 Nhibernate 与数据库进行通信。此外,我使用 FluentNhibernate 进行模型映射,但我被困在映射的某些部分。这是我在 ProductMap

中的映射 Class

松动地图

   public LosingMap()
        {
            Id(x => x.id);
            Map(x => x.reason);
            Map(x => x.quantity);
            Map(x => x.lose_sum);
            Map(x => x.rate);
            Map(x => x.deleted);
            Map(x => x.create_date);
            Map(x => x.update_date);
            References(x => x.currency).Column("CurrencyCode");
            Table("Loosing");


        }
    }

这是货币地图

货币地图

public CurrencyMap()
    {

        Id(x => x.id);
        Map(x => x.code).UniqueKey("currency_code");


        Map(x => x.name);
        Map(x => x.shname);
        Map(x => x.symbol);
        Map(x => x.deleted);
        HasMany(x => x.Losings).Cascade.All();
        Table("currency");
    }
}

这是我的货币 table 一个主键和一个唯一键如何在我的 LoosingMap 中引用 class 我的唯一键而不是主键 ???

CurrencyMap 上使用 KeyColumn 使用:

public CurrencyMap()
    {

        Map(x => x.id); // Optional
        KeyColumn(x => x.code)


        Map(x => x.name);
        Map(x => x.shname);
        Map(x => x.symbol);
        Map(x => x.deleted);
        HasMany(x => x.Losings).Cascade.All();
        Table("currency");
    }
}

根据FluentNHibernate "HasMany / one-to-many" documentation,"As with References, the foreign-key defaults to Author_id, and you can override it with the KeyColumn method or change the default behaviour with a Conventions.",所以你也可以使用:

HasMany(x => x.Losings).KeyColumn(x => x. CurrencyCode).Cascade.All();