【问题标题】:Fluent NHibernate automapping composite id with componentFluent NHibernate 使用组件自动映射复合 ID
【发布时间】:2012-11-13 17:30:28
【问题描述】:

我有这种复杂的情况:一个国家/地区/州/城市的数据库,其主键由一个名为“Id”的列中的代码 (nvarchar(3)) 加上“祖先”的所有键列(地区/州/城市)。

所以表国家只有一个键列(Id),而城市有4个键列(Id、StateId、regionId、CountryId)。显然它们都是相关的,因此每个祖先列都是相关表的外键。

我的模型中有实体可以映射这种关系。但它们都派生自一种称为 Entity 的类型,其中 T 可能是简单类型(字符串、in 等)或复杂类型(实现键的组件)。 Entity 实现了一个名为 Id 的 T 类型属性。

对于每个 db 表,如果它有一个复杂的键,我会在一个单独的组件中实现它,该组件还覆盖 Equals 和 GetHashCode() 方法(将来我将在 Entity 基类中实现它们)。

所以我有一个 RegionKey 组件,它有 2 个属性(Id 和 CountryId)。 我有外键和主键命名和类型的约定,没关系。 我还为每个复杂实体映射了 ovverrides。

为简单起见,我们只关注国家和地区表。他们在这里:

public class Country: Entity<string>
{
    public virtual string Name { get; set; }
    public virtual IList<Region> Regions { get; set; } 
}

 public class Region: Entity<RegionKey>
{
    public virtual string Name { get; set; }
    public virtual Country Country { get; set; }
}

和 RegionKey 组件:

namespace Hell.RealHellState.Api.Entities.Keys
{
  [Serializable]
  public class RegionKey
  {
    public virtual string Id { get; set; }
    public virtual string CountryId { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as RegionKey;
        if (t == null)
            return false;
        return Id == t.Id && CountryId == t.CountryId;
    }

    public override int GetHashCode()
    {
        return (Id + "|" + CountryId).GetHashCode();
    } 
  }
}

AutoPersistenceModel的配置如下:

    public ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
          .Database(
            MsSqlCeConfiguration.Standard
            .ConnectionString(x=>x.Is(_connectionString))
          )
          .Mappings(m => m.AutoMappings.Add(AutoMappings))
          .ExposeConfiguration(BuildSchema)
          .BuildSessionFactory();
    }

    private AutoPersistenceModel AutoMappings()
    {
        return AutoMap.Assembly(typeof (Country).Assembly)
            .IgnoreBase(typeof(Entity<>))
            .Conventions.AddFromAssemblyOf<DataFacility>()
            .UseOverridesFromAssembly(GetType().Assembly)
            .Where(type => type.Namespace.EndsWith("Entities"));
    }

    private static void BuildSchema(Configuration config)
    {
        //Creates database structure
        new SchemaExport(config).Create(false, true);
        //new SchemaUpdate(config).Execute(false, true);
    }

这里是 Regions 实体覆盖

public class RegionMappingOverride : IAutoMappingOverride<Region>
{
    public void Override(AutoMapping<Region> mapping)
    {
        mapping.CompositeId(x=>x.Id)
            .KeyProperty(x => x.Id, x=> x.ColumnName("Id").Length(3).Type(typeof(string)))
            .KeyProperty(x => x.CountryId, x => x.ColumnName("CountryId").Length(3).Type(typeof(string)));
    }
}

现在,当我测试此映射时,我收到一条错误消息:关系中列的数据类型不匹配。

我也试过这个覆盖:

    public void Override(AutoMapping<Region> mapping)
    {
        mapping.CompositeId()
            .ComponentCompositeIdentifier(x=>x.Id)
            .KeyProperty(x => x.Id.Id, x=> x.ColumnName("Id").Length(3).Type(typeof(string)))
            .KeyProperty(x => x.Id.CountryId, x => x.ColumnName("CountryId").Length(3).Type(typeof(string)));
    }

它几乎可以工作,但它创建了一个带有 varbinary(8000) 单列键的 Regions 表,这不是我想要的:

CREATE TABLE [hell_Regions] (
[Id] varbinary(8000) NOT NULL
, [Name] nvarchar(50) NULL
, [CountryId] nvarchar(3) NULL
);
GO
ALTER TABLE [hell_Regions] ADD CONSTRAINT [PK__hell_Regions__0000000000000153] PRIMARY KEY ([Id]);
GO
ALTER TABLE [hell_Regions] ADD CONSTRAINT [FK_Regions_Country] FOREIGN KEY ([CountryId]) REFERENCES [hell_Countries]([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

我不知道如何处理它,因为在我看来一切都很好。

提前感谢您的回答

【问题讨论】:

    标签: fluent-nhibernate fluent-nhibernate-mapping


    【解决方案1】:

    好的,我设法解决了这个问题:我必须将 CompositeId 类签名为 MAPPED,因为它是一个组件。所以这是我的新 RegionMappingOverride:

    public class RegionMappingOverride : IAutoMappingOverride<Region>
    {
        public void Override(AutoMapping<Region> mapping)
        {
            mapping.CompositeId(x=>x.Id)
                .Mapped()
                .KeyProperty(x =>x.Id,x=>x.Length(3))
                .KeyProperty(x => x.CountryId, x=>x.Length(3));
        }
    
    }
    

    现在创建的sql是正确的:

    create table hell_Countries (
        Id NVARCHAR(3) not null,
       Name NVARCHAR(50) null,
       primary key (Id)
    )
    
    create table hell_Regions (
        Id NVARCHAR(3) not null,
       CountryId NVARCHAR(3) not null,
       Name NVARCHAR(50) null,
       primary key (Id, CountryId)
    )
    
    alter table hell_Regions 
        add constraint FK_Region_Country 
        foreign key (CountryId) 
        references hell_Countries
    

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 2011-02-10
      • 2013-12-01
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多