Darius Kucinskas
Darius Kucinskas

Reputation: 10661

FluentNHibernate and custom TableNameConvention

FluentNHibernate version 1.3.0.727

I have the following custom TableNameConvention:

public class TableNameConvention : IClassConvention, IClassConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
    {
        criteria.Expect(x => x.TableName, Is.Not.Set);
    }

    public void Apply(IClassInstance instance)
    {
        instance.Table(instance.EntityType.Name + "s");
    }
}

I have the following entity mapping:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Email).Not.Nullable().Length(200);
        Map(x => x.FirstName).Length(100);
        Map(x => x.LastName).Length(100);
        Map(x => x.Password).Not.Nullable().Length(30);
    }
}

I'm generating database like this:

var configuration = Fluently.Configure()
    .Mappings(m => m.FluentMappings
        .AddFromAssemblyOf<IEntity>()
                              .Conventions.Add<TableNameConvention>())
        .BuildConfiguration();

var schema = new SchemaExport(configuration);
schema.Drop(false, true);
schema.Create(false, true);

Then generating database User entity table is still generated as User but not Users as I want it to be. It seams that Accept method fails. Is this FluentNHibernate bug?

Upvotes: 1

Views: 324

Answers (1)

Firo
Firo

Reputation: 30803

Remove this code

public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
{
    criteria.Expect(x => x.TableName, Is.Not.Set);
}

because it is not needed and maybe even not what you want. FNH has three values internally for each property (e.g. tablename)

  1. value set explicitly in classmap
  2. value set by convention
  3. defaultvalue

and it uses what it finds in this order valueInEffect = explicitValue ?? conventionValue ?? defaultValue

Upvotes: 1

Related Questions