流畅的 NHibernate 映射

发布于 2024-10-07 23:11:14 字数 794 浏览 2 评论 0原文

我有这些类:

public class Entity
{
  public virtual Guid Id { get; set; }
  public virtual string EntityName { get; set; }
  public virtual IDictionary<string, Property> { get; set; }
  // ...
}

public class Property
{
  public virtual int? IntValue { get; set; }
  public virtual decimal? DecimalValue { get; set; }
}

是否可以创建 Fluent NHibernate 映射,以便执行结果模式将给出这些表:

[Entities]
* Id : UNIQUEIDENTIFIER NOT NULL
* EntityName : NVARCHAR(50) NOT NULL
with a clustered index on "Id"

[Properties]
* EntityId : UNIQUEIDENTIFIER NOT NULL
* PropertyName : VARCHAR(50) NOT NULL
* IntValue : INT NULL
* DecimalValue : DECIMAL(12,6) NULL
with a clustered index on "EntityId" and "PropertyName"

或者我需要更改我的类吗?

比是/否更详细的答案将不胜感激:)

I have these classes:

public class Entity
{
  public virtual Guid Id { get; set; }
  public virtual string EntityName { get; set; }
  public virtual IDictionary<string, Property> { get; set; }
  // ...
}

public class Property
{
  public virtual int? IntValue { get; set; }
  public virtual decimal? DecimalValue { get; set; }
}

Is it possible to create Fluent NHibernate mappings so that executing the resulting schema will give these tables:

[Entities]
* Id : UNIQUEIDENTIFIER NOT NULL
* EntityName : NVARCHAR(50) NOT NULL
with a clustered index on "Id"

[Properties]
* EntityId : UNIQUEIDENTIFIER NOT NULL
* PropertyName : VARCHAR(50) NOT NULL
* IntValue : INT NULL
* DecimalValue : DECIMAL(12,6) NULL
with a clustered index on "EntityId" and "PropertyName"

Or do I need to change my classes?

An answer more verbose than yes/no will be much appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧梦荧光笔 2024-10-14 23:11:14

除了需要手动创建的聚集索引之外,是的。您是否绝对需要 FNH 来生成您的架构?

为什么不自己生成特定于您的需求的模式,然后相应地映射它。

(未经测试或任何东西,写在我的脑海中)

public class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        Table("Entities");

        Id(x => x.Id).GeneratedBy.GuidComb();
        Map(x => x.Name).CustomSqlType("NVARCHAR").Length(50).Not.Nullable();
        HasMany<Property>(x => x.Properties)
            .Table("Properties")
            .KeyColumn("PropertyName")
            .Inverse()
            .AsBag();
    }
}

public class PropertyMap : ClassMap<Property>
{
    public PropertyMap()
    {
        Table("Properties");

        Id(x => x.Id).GeneratedBy.GuidComb();
        Map(x => x.PropertyName).Length(50).Not.Nullable();
        Map(x => x.IntValue);
        Map(x => x.DecimalValue);
    }
}

Besides your clustered index which you would need to manually create, yes. Do you absolutely require FNH to generate your schema?

Why don't you just generate the schema yourself specific to your requirements and then map it accordingly.

(not tested or anything, written off the top of my head)

public class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        Table("Entities");

        Id(x => x.Id).GeneratedBy.GuidComb();
        Map(x => x.Name).CustomSqlType("NVARCHAR").Length(50).Not.Nullable();
        HasMany<Property>(x => x.Properties)
            .Table("Properties")
            .KeyColumn("PropertyName")
            .Inverse()
            .AsBag();
    }
}

public class PropertyMap : ClassMap<Property>
{
    public PropertyMap()
    {
        Table("Properties");

        Id(x => x.Id).GeneratedBy.GuidComb();
        Map(x => x.PropertyName).Length(50).Not.Nullable();
        Map(x => x.IntValue);
        Map(x => x.DecimalValue);
    }
}
嘴硬脾气大 2024-10-14 23:11:14

这是我提出的映射:

public sealed class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        Table("Entities");

        Id(c => c.Id);
        Map(c => c.EntityName).CustomSqlType("nvarchar(50)").Not.Nullable();

        HasMany(c => c.Properties)
            .KeyColumn("EntityId")
            .AsMap<string>("PropertyName")
            .Component(part =>
            {
                part.Map(x => x.IntValue);
                part.Map(x => x.DecimalValue).Precision(12).Scale(6);
            });
    }
}

架构生成产生了这个:

create table Entities (
   Id UNIQUEIDENTIFIER not null,
   EntityName nvarchar(50) not null,
   primary key (Id)
)

create table Properties (
   EntityId UNIQUEIDENTIFIER not null,
   IntValue INT null,
   DecimalValue DECIMAL(12, 6) null,
   PropertyName INT not null,
   primary key (EntityId, PropertyName)
)

alter table Properties 
    add constraint FK63646D8550C14DC4 
    foreign key (EntityId) 
    references Entities

这几乎是我所需要的,除了列顺序(小问题)和 PropertyName 是 nvarchar(255) 而不是varchar(50)(我真正关心的东西)。

Here's the mapping that I have come up with:

public sealed class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        Table("Entities");

        Id(c => c.Id);
        Map(c => c.EntityName).CustomSqlType("nvarchar(50)").Not.Nullable();

        HasMany(c => c.Properties)
            .KeyColumn("EntityId")
            .AsMap<string>("PropertyName")
            .Component(part =>
            {
                part.Map(x => x.IntValue);
                part.Map(x => x.DecimalValue).Precision(12).Scale(6);
            });
    }
}

Schema generation yields this:

create table Entities (
   Id UNIQUEIDENTIFIER not null,
   EntityName nvarchar(50) not null,
   primary key (Id)
)

create table Properties (
   EntityId UNIQUEIDENTIFIER not null,
   IntValue INT null,
   DecimalValue DECIMAL(12, 6) null,
   PropertyName INT not null,
   primary key (EntityId, PropertyName)
)

alter table Properties 
    add constraint FK63646D8550C14DC4 
    foreign key (EntityId) 
    references Entities

Which is pretty much what I need, with an exception of the column order (minor issue) and PropertyName being nvarchar(255) instead of varchar(50) (something I actually care about).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文