NHibernate ClassMapping自动创建一个唯一的键

本文关键字:一个 唯一 ClassMapping 创建 NHibernate | 更新日期: 2023-09-27 18:08:08

我使用NHibernate与ClassMapping。我在其中一个属性上创建了一个索引,像这样:

public class ShopMapping : ClassMapping<Shop>
    {
        public ShopMapping()
        {
            Table("Shops");
            Id(p => p.Id, m => m.Generator(NHibernate.Mapping.ByCode.Generators.GuidComb));
            Property(p => p.CountryCode, m =>
            {
                m.Length(10);
                m.NotNullable(true);
                m.Index("ShopCountryCodeIdx");
                m.Unique(false);
            });
        }
    }

这会生成名为ShopCountryCodeIdx的索引,但我还在同一列上获得了一个名为'CountryCode'的唯一索引。我尝试了一个没有m.Unique(错误),但没有效果。我刚换了台新电脑。旧pc: 32位VS2012和新pc: 64位VS2015。nhibernate版本是相同的(3.4.1.4)。数据库是MySQL 5.7(在旧机器上我使用MySQL 5.5)。这怎么可能呢?

NHibernate ClassMapping自动创建一个唯一的键

发现部分问题。在我的另一个ClassMapping类中,我有一个跨三列的唯一索引。其中一个也是CountryCode列。这就导致了这种奇怪的行为。

Property(p => p.CountryCode, m =>
        {
            m.Length(10);
            m.NotNullable(true);
            m.Index("PanelShopCountryCodeIdx");
            //m.UniqueKey("UK_psPanelShopnrIdx");
        });
        Property(p => p.ShopNr, m =>
        {
            m.Length(25);
            m.NotNullable(true);
            //m.UniqueKey("UK_psPanelShopnrIdx");
        });
        Property(p => p.PanelCode, m =>
        {
            m.Length(25);
            m.NotNullable(true);
           // m.UniqueKey("UK_psPanelShopnrIdx");
        });

摆脱那些m.u uniquekey语句,解决了问题(这让我面临另一个挑战,如何在这种情况下创建跨三列的唯一索引,其中一列也有自己的(非唯一)索引…