Fluent NHibernate - 设置 CutomType IIdConvention

发布于 2024-10-01 20:19:51 字数 1491 浏览 2 评论 0原文

我有以下用于 FluentNHibernate 自动映射的 IIdConvention。我希望所有 id 属性都使用由字符串属性表示的自定义类型,但 CustomType 永远不会应用于我的映射。

public class PrimaryKeyHasTableName : FluentNHibernate.Conventions.IIdConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
    {           
        instance.Column(instance.EntityType.Name + "Id");
        instance.CustomType<CustomIdType>();
    }
}

当我查看 FluentNHibernate 源代码时,似乎 id 属性的类型已经设置,因此我的约定没有设置它。

如果我使用 ClassMap 手动映射类,则为 Identity 属性设置 CustomType 不会出现问题。

 Id(x => x.Id)
      .Column("UserId")                
      .CustomType<OnFileIdType>();

有谁知道如何使用约定成功设置自定义 id 属性?

或者让我的约定在映射过程中尽早运行,以便在我的代码运行时尚未设置类型。

另外,这是我的配置代码:

Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005.ConnectionString(connString))
            .Mappings(m =>
            {
                m.FluentMappings.AddFromAssemblyOf<BaseEntity>();
                m.AutoMappings.Add(AutoMap.AssemblyOf<BaseEntity>()
                                  .Where(t => t.Namespace.EndsWith("Models.Domain"))
                                  .Conventions.AddFromAssemblyOf<BaseEntity>()
                                  .UseOverridesFromAssemblyOf<BaseEntity>()
                );
            })
            .ExposeConfiguration(CreateSchema)
            .BuildSessionFactory();

谢谢。

I have the following IIdConvention for a FluentNHibernate automapping. I want all of my id properties to use a custom type that is represented by a string property but the CustomType is never applied to my mappings.

public class PrimaryKeyHasTableName : FluentNHibernate.Conventions.IIdConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
    {           
        instance.Column(instance.EntityType.Name + "Id");
        instance.CustomType<CustomIdType>();
    }
}

When I looked into the FluentNHibernate source it appears that the Type for the id property has already been set so it is not being set by my convention.

If I use a ClassMap to map the class manually I have not problem setting the CustomType for the Identity property.

 Id(x => x.Id)
      .Column("UserId")                
      .CustomType<OnFileIdType>();

Does anybody know how I can successfully set the custom id property using a convention?

Or get my convention to run earlier in the mapping process so that the Type isn't already set by the time my code runs.

Also, here's my configuration code:

Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005.ConnectionString(connString))
            .Mappings(m =>
            {
                m.FluentMappings.AddFromAssemblyOf<BaseEntity>();
                m.AutoMappings.Add(AutoMap.AssemblyOf<BaseEntity>()
                                  .Where(t => t.Namespace.EndsWith("Models.Domain"))
                                  .Conventions.AddFromAssemblyOf<BaseEntity>()
                                  .UseOverridesFromAssemblyOf<BaseEntity>()
                );
            })
            .ExposeConfiguration(CreateSchema)
            .BuildSessionFactory();

Thanks.

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

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

发布评论

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

评论(3

不爱素颜 2024-10-08 20:19:51

我不认为你可以通过约定来实现你想要的。

您可以尝试的一件事是让所有实体从定义 Id 属性并具有自定义类型映射的抽象实体中继承。
不过,我不推荐这样做,它只会为更多自动映射问题打开空间。

只需为每个类映射手动查找 CustomType 即可。

I don't think you can achieve what you want with conventions.

One thing you can try is having all your entities subclass from an abstract entity that defines the Id property and has the custom type mapping for it.
I wouldn't recommend this however, It will just open room for more automapping problems.

Just go for the manual CustomType for each class map.

幸福%小乖 2024-10-08 20:19:51

我遇到了完全相同的问题,如果 FNH 的 IIdConvention 支持这一点那就太好了。

在尝试了一些事情并阅读了本文之后,我已经放弃了为使用自定义类型作为 Id 的实体实现 IAutoMappingOverride 的想法。

public class ProductMap : IAutoMappingOverride<Product>
{
    public void Override(AutoMapping<Product> mapping)
    {
        mapping.Id(x => x.Id).CustomType<ProductId>();
    }
}

使用自动映射覆盖而不是 ClassMap 将继续自动映射其余属性。更改您的 AutoMappings 初始化代码以包括

.Overrides.AddFromAssemblyOf<BaseEntity>()

I'm having the exact same problem, it would be great if FNH's IIdConvention supported this.

After trying a few things and reading this I have resigned myself to implementing IAutoMappingOverride for entities using a custom type for their Id.

public class ProductMap : IAutoMappingOverride<Product>
{
    public void Override(AutoMapping<Product> mapping)
    {
        mapping.Id(x => x.Id).CustomType<ProductId>();
    }
}

Using automapping override instead of ClassMap will continue to automap the rest of your properties. Alter your AutoMappings initialisation code to include

.Overrides.AddFromAssemblyOf<BaseEntity>()
━╋う一瞬間旳綻放 2024-10-08 20:19:51

我遇到了同样的问题。我相信它是由 Conventions.AddFromAssemblyOf() 的调用引起的,这导致您的自定义约定不被应用。

(顺便说一句,仅当您的约定类与 BaseEntity 位于同一程序集中时,发布的代码才有效 - 使用 Conventions.AddFromAssemblyOf() 可能更安全)

提出了一个错误关于 AddFromAssemblyOf 早在 2011 年 2 月:Fluent NHibernate - 设置 CutomType IIdConvention。没有发布任何解决方案。

解决方法似乎是显式添加每个约定。

Conventions.Add<PrimaryKeyHasTableName>()

要检查这一点,您可以在添加自动映射的行后面添加以下行,以便将映射导出到硬盘驱动器上的 hbm.xml 文件,并且查看生成的映射。

m.AutoMappings.ExportTo(@"C:\ExportedMappings")

另外,我建议您在 Apply 方法中添加一个断点并运行代码以确保它被调用。)

I am getting the same problem. I believe that it is arising from the invocation of Conventions.AddFromAssemblyOf<BaseEntity>(), which is causing your custom convention to not be applied.

(By the way, the posted code will work only if your convention classes are in the same assembly as your BaseEntity - it might be safer to use Conventions.AddFromAssemblyOf<PrimaryKeyHasTableName>())

A bug was raised about AddFromAssemblyOf back in February 2011: Fluent NHibernate - Setting CutomType IIdConvention. There is no resolution posted.

The work-around appears to be to add each of the Conventions explicitly

Conventions.Add<PrimaryKeyHasTableName>()

To check this, you can add the following line after the line which adds the automappings, in order to export the mappings to an hbm.xml file on your hard drive, and take a look at the generated mappings.

m.AutoMappings.ExportTo(@"C:\ExportedMappings")

Also, I suggest you add a breakpoint into the Apply method and run the code to ensure that it is being invoked.)

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