Free NHibernate table naming convention does not work

I have the following convention that I load into my FNH configuration

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 in any of my mappings, but this convention does not apply. I am using Fluent NHibernate 1.4.1.1. Can someone discover something that I might have done wrong?

UPDATE

Agreements are downloaded as follows:

 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 are hosted in the same assembly and namespace as HiLoConvention, mentioned above in the call to the .AddFromAssembly () method.

UPDATE 2:

The problem is the Accept () method, because if I remove this method (as well as the IClassConventionAcceptance interface from the class declaration), then the convention will be applied. I also tried this wait to no avail

 criteria.Expect(x => string.IsNullOrEmpty(x.TableName)) 

Source code worked with Fluent 1.2.1 ...

+4
source share
2 answers

You tried

 m.FluentMappings.ConventionDiscovery.AddFromAssemblyOf<HiLoConvention>() 

instead

 m.FluentMappings.Conventions.AddFromAssemblyOf<HiLoConvention>() 
0
source

This question is old, but maybe it can help someone else:

I assume that you wanted to establish an agreement for each object, unless the table name was explicitly indicated on the map. To achieve this, 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 for all objects if the TableName is not explicitly indicated on the map.

0
source

Source: https://habr.com/ru/post/1446271/


All Articles