【问题标题】:NHibernate ClassMapping creates automatic a unique keyNHibernate ClassMapping 自动创建唯一键
【发布时间】:2016-09-21 09:53:54
【问题描述】:

我使用 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(false) 的情况,但没有效果。 我刚搬到一台新电脑。旧电脑:32 位的 VS2012 和新电脑:64 位的 VS2015。 NHirate 版本是相同的(3.4.1.4)。数据库是 MySQL 5.7(在旧机器上我使用 MySQL 5.5)。 这怎么可能?

【问题讨论】:

  • 我只是重新运行代码(使用删除数据库对象等)并使用 m.Unique(false) 重新创建表。这仍然会产生一个额外的索引“CountryCode”(不是唯一的)。我忘了说 CountryCode 属性确实存在于几个对象中。所有这些与对象相关的表都有一个额外的索引“CountryCode”。
  • 编辑:我猜会遇到一些缓存问题。 CountryCode 索引仍然是“唯一的”... :(

标签: c# mysql nhibernate


【解决方案1】:

发现了部分问题。我的其他 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.UniqueKey 语句,解决了问题(这让我面临另一个挑战,如何在这种情况下创建跨三列的唯一索引,其中一列也有自己的(非唯一)索引.. .

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2017-08-11
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多