Fluent NHibernate table naming convention not working - nhibernate

I have the following convention which I load into my FNH config
public class TableNameConvention : IClassConvention, IClassConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
{
criteria.Expect(x => x.TableName, Is.Not.Set);
}
public void Apply(IClassInstance instance)
{
var tableName = instance.EntityType.Name.Pluralise();
instance.Table(tableName);
}
}
I do not specify table names on any of my mappings, yet this convention is not applied. I'm using Fluent NHibernate 1.4.1.1. Can anyone spot anything I might have done wrong?
UPDATE
The conventions are loaded in the following manner:
public static NHibernate.Cfg.Configuration BuildConfiguration()
{
var connectionStringName = "mydb";
return Fluently.Configure(new NHibernate.Cfg.Configuration())
.Database(MsSqlConfiguration
.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey(connectionStringName))
.Dialect<MsSql2008Dialect>()
.AdoNetBatchSize(50))
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<Profile>();
m.FluentMappings.Conventions.Add(DefaultLazy.Always(), DynamicUpdate.AlwaysTrue(), DynamicInsert.AlwaysTrue());
m.FluentMappings.Conventions.AddFromAssemblyOf<HiLoConvention>();
})
.ExposeConfiguration(config => config.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, typeof(ManagedWebSessionContext).AssemblyQualifiedName))
.ExposeConfiguration(HiLoConvention.CreateScript)
.ExposeConfiguration(RunSchemaUpdate)
.BuildConfiguration();
}
All conventions sit in the same assembly and namespace as the HiLoConvention referenced above in the .AddFromAssembly() method call.
UPDATE 2:
The problem is in the Accept() method, because if I remove this method (and also the IClassConventionAcceptance interface from the class declaration) then the convention is applied. I have also tried this expectation to no avail
criteria.Expect(x => string.IsNullOrEmpty(x.TableName))
The original code worked with Fluent 1.2.1...

This question is old, but perhaps this can help someone else:
I assume you wanted to set the convention on each entity, unless a table name was specified explicitly in the map. So to achieve that, you can simply do the following:
public class TableNameConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
var tableName = instance.EntityType.Name.Pluralise();
instance.Table(tableName);
}
}
This will apply the convention on all entities, unless TableName was specified explicitly in the map.

Have you tried
m.FluentMappings.ConventionDiscovery.AddFromAssemblyOf<HiLoConvention>()
in place of
m.FluentMappings.Conventions.AddFromAssemblyOf<HiLoConvention>()

Related

FluentNHibernate - Getting the entity name when mapping. Current method being made obsolete

I am currently automatically naming my tables in fluent NHibernate by using AutoMappingOverride's and setting the name of the table to the pluralised name of the entity like so.
public class PersonMappingOverride : IAutoMappingOverride<Person>
{
public void Override(AutoMapping<Person> mapping)
{
mapping.Table(mapping.EntityType.Name.Pluralize());
mapping.Id(x => x.PersonId).Column("PersonId").GeneratedBy.Increment();
mapping.Map(x => x.Name).CustomSqlType("nvarchar(200)");
}
}
public static string Pluralize(this string input)
{
return System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new CultureInfo("en-US")).Pluralize(input);
}
I am getting the following error.
'FluentNHibernate.Mapping.ClasslikeMapBase.EntityType' is obsolete: 'Do not call this method. Implementation detail mistakenly made public. Will be made private in next version.'
Does anyone know the correct way of getting the entity name? I've done a fair bit of Googling and have only found examples of people using this obsolete method to achieve what I need.
Cheers
Steve
Thanks for all the great help guys. Using conventions is way more elegant for my use case than the overrides anyway.
var sqlConfig = MsSqlConfiguration.MsSql2005.ConnectionString(ConnectionString);
var config = Fluently.Configure().Database(sqlConfig);
config.Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<User>(new IJAutomappingConfiguration()).Conventions.AddFromAssemblyOf<TableNamingConvention>()));
public class TableNamingConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(instance.EntityType.Name.Pluralize());
}
}
Here is the full code to get this working.
Here's how you would do it using a Convention:
public class NamingConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(instance.EntityType.Name.Pluralize());
}
}
Maybe trying Conventions, FluentNHibernate Conventions

Choose between Auto Mapping and Fluent mapping with Fluent NHibernate (S#arp Architecture)

I have a application using NHibernate Auto Mapping... All working fine so far...
My Fluent Global.asax config:
private void InitializeNHibernateSession()
{
NHibernateSession.Init(
webSessionStorage,
new string[] { Server.MapPath("~/bin/Proj.Data.dll") },
new AutoPersistenceModelGenerator().Generate(),
Server.MapPath("~/NHibernate.config"));
}
But I need to map a class with Fluent mapping... I created the class :
namespace Proj.Data.NHibernateMaps
{
public class CategoryMap : IAutoMappingOverride<Category>
{
public void Override(AutoMapping<Category> mapping)
{
mapping.Id(x => x.Id)
.GeneratedBy.Identity();
mapping.Map(x => x.Description);
mapping.Map(x => x.UrlName);
mapping.References(x => x.ParentCategory)
.Not.LazyLoad();
}
}
}
The problem is that this mapping is never used by the NHibernate... Instead it uses the Auto Mapping generated Category...
How can I use my Fluent Mapping ?
Thanks
Paul
Wherever you're configuring the AutoPersistenceModel you need to reference the mapping overrides. I find the easiest way to do this is to just point it at the assembly containing the mapping overrides and let it discover all of them. That way you can add new IAutoMappingOverride implementations and it will be automatically picked up. You do this using the UseOverridesFromAssemblyOf extension method.
public class AutoPersistenceModelGenerator {
public AutoPersistenceModel Generate() {
return AutoMap.AssemblyOf<Category>()
.UseOverridesFromAssemblyOf<CategoryMap>();
}
}

Why is Fluent NHibernate ignoring my convention?

I have a convention UserTypeConvention<MyUserType> where MyUserType : IUserType where MyUserType handles an enum type MyEnum. I have configured Fluent NHibernate thusly
sessionFactory = Fluently
.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(
c => c.Is(connectionString))
)
.Mappings(
m => m
.FluentMappings
.AddFromAssemblyOf<A>()
.Conventions
.AddFromAssemblyOf<A>()
)
.BuildSessionFactory();
where A is a type in the same assembly as UserTypeConvention<MyUserType> and MyUserType. However, Fluent NHibernate is not applying MyUserType to properties of type MyEnum on my domain objects. Instead, it is applying FluentNHibernate.Mapping.GenericEnumMapper<MyEnumType> to these properties.
What is going on?
For now I have solved this with:
public class MyEnumUserTypeConvention : UserTypeConvention<MyEnumUserType> {
public override void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) {
// Fluent NHibernate is too eager in applying GenericEnumMapper
// so our criteria is that it is already applied this type
criteria.Expect(x => x.Type == typeof(GenericEnumMapper<MyEnum>));
}
public override void Apply(IPropertyInstance instance) {
// we override Fluent NHibernate's application of GenericEnumMapper
instance.CustomType<MyEnumUserType>();
}
}
I think this should be thoroughly unnecessary. If someone told me this were a bug in Fluent NHibernate, that'd be fine. If someone gave me a good reason why Fluent NHibernate should be so eager in applying GenericEnumMapper that would be acceptable too.
Ok i tried the following and I think it will works for you :
just overriede the Accept method in MyEnumUserTypeConvention class and do nothing inside it:
public class MyEnumUserTypeConvention : UserTypeConvention<MyEnumUserType>
{
public override void Accept(FluentNHibernate.Conventions.AcceptanceCriteria.IAcceptanceCriteria<FluentNHibernate.Conventions.Inspections.IPropertyInspector> criteria)
{
///Do nothing
}
}

Fluent NHibernate: Column mapping convention

I'm having some problems with getting the following convention to work:
public class ColumnNameUpperConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
string cName = instance.Property.Name.ToUpper();
instance.Column(cName);
}
}
What I'm wanting the above code to do is to map a property called "Modified" to a column named "MODIFIED".
And this is my config:
var config =
Fluently.Configure()
.Database(OracleClientConfiguration.Oracle10
.ConnectionString(c => c.FromConnectionStringWithKey(cstringName)))
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<AgilityObject>();
m.AutoMappings.Add(
AutoMap.AssemblyOf<AgilityObject>(mappingConfiguration)
.Conventions.Add<ColumnNameUpperConvention>());
})
.ExposeConfiguration(x => x.SetProperty("current_session_context_class", "thread_static"))
.ExposeConfiguration(x => x.SetProperty("generate_statistics", "true"))
.BuildSessionFactory();
When I debug I can see that my convention code gets executed, but it doesn't seem like it actually does anything.
Am I missing something?
PS. Do the mappings I've set explicitly in a ClassMap automatically override conventions? There are exceptions to the above convention and I want to map those properties by hand.
My bad. I needed to use IAutoMappingOverride instead of ClassMap if I am to use AutoMapping at all.

How do you map an enum as an int value with fluent NHibernate?

Question says it all really, the default is for it to map as a string but I need it to map as an int.
I'm currently using PersistenceModel for setting my conventions if that makes any difference.
Update
Found that getting onto the latest version of the code from the trunk resolved my woes.
The way to define this convention changed sometimes ago, it's now :
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
So, as mentioned, getting the latest version of Fluent NHibernate off the trunk got me to where I needed to be. An example mapping for an enum with the latest code is:
Map(quote => quote.Status).CustomTypeIs(typeof(QuoteStatus));
The custom type forces it to be handled as an instance of the enum rather than using the GenericEnumMapper<TEnum>.
I'm actually considering submitting a patch to be able to change between a enum mapper that persists a string and one that persists an int as that seems like something you should be able to set as a convention.
This popped up on my recent activity and things have changed in the newer versions of Fluent NHibernate to make this easier.
To make all enums be mapped as integers you can now create a convention like so:
public class EnumConvention : IUserTypeConvention
{
public bool Accept(IProperty target)
{
return target.PropertyType.IsEnum;
}
public void Apply(IProperty target)
{
target.CustomTypeIs(target.PropertyType);
}
public bool Accept(Type type)
{
return type.IsEnum;
}
}
Then your mapping only has to be:
Map(quote => quote.Status);
You add the convention to your Fluent NHibernate mapping like so;
Fluently.Configure(nHibConfig)
.Mappings(mappingConfiguration =>
{
mappingConfiguration.FluentMappings
.ConventionDiscovery.AddFromAssemblyOf<EnumConvention>();
})
./* other configuration */
Don't forget about nullable enums (like ExampleEnum? ExampleProperty)! They need to be checked separately. This is how it's done with the new FNH style configuration:
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum ||
(x.Property.PropertyType.IsGenericType &&
x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
this is how I've mapped a enum property with an int value:
Map(x => x.Status).CustomType(typeof(Int32));
works for me!
For those using Fluent NHibernate with Automapping (and potentially an IoC container):
The IUserTypeConvention is as #Julien's answer above: https://stackoverflow.com/a/1706462/878612
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
The Fluent NHibernate Automapping configuration could be configured like this:
protected virtual ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(SetupDatabase)
.Mappings(mappingConfiguration =>
{
mappingConfiguration.AutoMappings
.Add(CreateAutomappings);
}
).BuildSessionFactory();
}
protected virtual IPersistenceConfigurer SetupDatabase()
{
return MsSqlConfiguration.MsSql2008.UseOuterJoin()
.ConnectionString(x =>
x.FromConnectionStringWithKey("AppDatabase")) // In Web.config
.ShowSql();
}
protected static AutoPersistenceModel CreateAutomappings()
{
return AutoMap.AssemblyOf<ClassInAnAssemblyToBeMapped>(
new EntityAutomapConfiguration())
.Conventions.Setup(c =>
{
// Other IUserTypeConvention classes here
c.Add<EnumConvention>();
});
}
*Then the CreateSessionFactory can be utilized in an IoC such as Castle Windsor (using a PersistenceFacility and installer) easily. *
Kernel.Register(
Component.For<ISessionFactory>()
.UsingFactoryMethod(() => CreateSessionFactory()),
Component.For<ISession>()
.UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
.LifestylePerWebRequest()
);
You could create an NHibernate IUserType, and specify it using CustomTypeIs<T>() on the property map.
You should keep the values as int / tinyint in your DB Table. For mapping your enum you need to specify mapping correctly. Please see below mapping and enum sample,
Mapping Class
public class TransactionMap : ClassMap Transaction
{
public TransactionMap()
{
//Other mappings
.....
//Mapping for enum
Map(x => x.Status, "Status").CustomType();
Table("Transaction");
}
}
Enum
public enum TransactionStatus
{
Waiting = 1,
Processed = 2,
RolledBack = 3,
Blocked = 4,
Refunded = 5,
AlreadyProcessed = 6,
}