gpt4 book ai didi

fluent-nhibernate - Fluent NHibernate 自动映射复合 id 与组件

转载 作者:行者123 更新时间:2023-12-04 07:42:29 24 4
gpt4 key购买 nike

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

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

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

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

所以我有一个具有 2 个属性(Id 和 CountryId)的 RegionKey 组件。我有外键和主键命名和类型的约定,这没关系。我还为每个复杂的实体映射了 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),这不是我想要的:

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

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

预先感谢您的回答

最佳答案

好的,我设法解决了它:我必须将 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

关于fluent-nhibernate - Fluent NHibernate 自动映射复合 id 与组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13561455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com