流畅的NHibernate自动化复合id与组件

时间:2012-11-26 08:56:10

标签: fluent-nhibernate fluent-nhibernate-mapping

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

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

我的模型中有实体来映射这种关系。但它们都来自一种称为实体< T>的类型。其中T可以是简单类型(字符串,等)或复杂类型(实现密钥的组件)。 实体LT; T>实现一个名为Id的类型为T的属性。

对于每个db表,如果它有一个comlex键,我在一个单独的组件中实现它,它也包含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)));
    }

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

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

我不清楚如何处理它,因为在我看来,每个人都可以。

提前感谢您的回答

1 个答案:

答案 0 :(得分:4)

好的我曾经解决过这个问题:我必须将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