Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent: Table name different from entity name

I am trying to use the automapping feature of Fluent with nHinbernate to map a class with a different name than the table itself is name.

(This is purely for stylistic reasons we have a class named Foo which contains an object named Bar but the table name is FooBar. We would rather not have a property Foo.FooBar.)

I can't find anything detailing how to give Fluent a clue on this change.

like image 888
Matt Avatar asked Apr 28 '26 09:04

Matt


2 Answers

With classmap you can specify the table name in the mapping.

public class BarMap : ClassMap<Bar>
{
    public BarMap()
    {
        Table("FooBar");
    }
}

With automap you can override the table name.

.Mappings( m => m.AutoMappings.Add( AutoMap.AssemblyOf<Bar>()
    .Override<Bar>( b => {
        b.Table("FooBar");
}))

You can also use conventions to affect table naming of all entities.

like image 77
Lachlan Roche Avatar answered Apr 29 '26 22:04

Lachlan Roche


You can specify the table name in the mapping. So it will look something like this:

public class FooMap : ClassMap<Foo>
{
  Table("FooBar");

  // Rest of your mapping goes here.
}
like image 29
lancscoder Avatar answered Apr 29 '26 21:04

lancscoder