1

according to this Post it is possible to change the naming convention from "[TableName]_id" to "[TableName]ID". However when I saw the code, I wasn't able to do it with my rather new (about 6 weeks old) version of Fluent NHibernate.

Original code:

var cfg = new Configuration().Configure();
var persistenceModel = new PersistenceModel();
persistenceModel.addMappingsFromAssembly(Assembly.Load("Examinetics.NHibernate.Data"));
persistenceModel.Conventions.GetForeignKeyNameOfParent = type => type.Name + "ID";
persistenceModel.Configure(cfg);
factory = cfg.BuildSessionFactory();

I came up with this:

PersistenceModel model = new PersistenceModel();
model.AddMappingsFromAssembly(assemblyType.Assembly);
model.ConventionFinder.Add(
    ConventionBuilder.Reference.Always(part
        => part.ColumnName(part.EntityType.Name + part.Property.Name)));

configuration.ExposeConfiguration(model.Configure);
return configuration.BuildConfiguration();

but this gives me the original Table, because EntityType is not the referenced Entity and I see no property to get the "parent" entity.

How can I do this?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Daniel Fabian
  • 3,828
  • 2
  • 19
  • 28

1 Answers1

0

See The answer about removing underscores in Ids using Fluent NHibernate Automapping conventions here.

A portion of the text, the tables and classes are in the link.

Solution:

Add a convention, it can be system wide, or more restricted.

ForeignKey.EndsWith("Id")

Code example:

var cfg = new StoreConfiguration();
var sessionFactory = Fluently.Configure()
  .Database(/* database config */)
  .Mappings(m =>
    m.AutoMappings.Add(
      AutoMap.AssemblyOf<Product>(cfg)
          .Conventions.Setup(c =>
              {
                  c.Add(ForeignKey.EndsWith("Id"));
              }
    )
  .BuildSessionFactory();

Now it will automap the ShelfId column to the Shelf property in Product.


Community
  • 1
  • 1
lko
  • 8,161
  • 9
  • 45
  • 62