开发者

Fluent NHibernate - Setting CutomType IIdConvention

开发者 https://www.devze.com 2023-01-24 19:18 出处:网络
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

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>()
                         开发者_如何学Go         .UseOverridesFromAssemblyOf<BaseEntity>()
                );
            })
            .ExposeConfiguration(CreateSchema)
            .BuildSessionFactory();

Thanks.


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.


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>()


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号