Fluent nhibernate Fluent NHibernate将复合id与组件自动映射
我有这样一个复杂的情况:一个国家/地区/州/城市的数据库,其主键由一个名为“Id”的列中的代码(nvarchar(3))加上“祖先”(地区/州/城市)的所有键列组成 因此,表country只有一个键列(Id),而cities有4个键列(Id、StateId、regionId、CountryId)。显然,它们都是相关的,所以每个祖先列都是相关表的外键 我的模型中有映射这种关系的实体。但它们都来自一个名为Entity的类型,其中T可能是简单类型(字符串、in等)或复杂类型(实现键的组件)。 实体实现一个名为T类型Id的属性 对于每个db表,如果它有一个comlex键,我将在一个单独的组件中实现它,该组件还包含Equals和GetHashCode()方法(将来我将在实体基类中实现这些方法) 所以我有一个RegionKey组件,它有两个属性(Id和CountryId)。 我有外键和主键命名和类型的约定,这是可以的。 我还为每个复杂实体映射了OVVERRIDS 为了简单起见,让我们只关注国家和地区表。这是:Fluent nhibernate Fluent NHibernate将复合id与组件自动映射,fluent-nhibernate,fluent-nhibernate-mapping,Fluent Nhibernate,Fluent Nhibernate Mapping,我有这样一个复杂的情况:一个国家/地区/州/城市的数据库,其主键由一个名为“Id”的列中的代码(nvarchar(3))加上“祖先”(地区/州/城市)的所有键列组成 因此,表country只有一个键列(Id),而cities有4个键列(Id、StateId、regionId、CountryId)。显然,它们都是相关的,所以每个祖先列都是相关表的外键 我的模型中有映射这种关系的实体。但它们都来自一个名为Entity的类型,其中T可能是简单类型(字符串、in等)或复杂类型(实现键的组件)。 实体实现
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; }
}
以下是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);
}
我不知道该怎么处理,因为在我看来一切都很好
提前感谢您的回答好的,我想解决它:我必须按照映射对CompositeId类进行签名,因为它是一个组件。这就是我的新区域AppingOverride:
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));
}
}
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)));
}
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
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));
}
}
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