I think this must have been asked before, but I searched for an hour, but could not find the answer.
Let's say I have the following 2 models:
public class Organism { public virtual int Id { get; private set; } } public class Animal : Organism { }
I want Fluent NHibernate to create the following tables for me:
OrganismTable OrganismId AnimalTable AnimalId
This can be easily achieved using manual matching:
public class OrganismMappinig : ClassMap<Organism> { public OrganismMappinig() { Table("OrganismTable"); Id(x => x.Id).Column("OrganismId"); } } public class AnimalMapping : SubclassMap<Animal> { public AnimalMapping() { Table("AnimalTable"); KeyColumn("AnimalId"); } }
But I canβt get the same result using a car. I tried to add the following conventions:
public class TableNameConvension : IClassConvention, IClassConventionAcceptance { public void Apply(IClassInstance instance) { instance.Table(instance.EntityType.Name + "Table"); } public void Accept(IAcceptanceCriteria<IClassInspector> criteria) { criteria.Expect(x => x.TableName, Is.Not.Set); } } public class PrimaryKeyNameConvention : IIdConvention { public void Apply(IIdentityInstance instance) { instance.Column(instance.EntityType.Name + "Id"); } }
He created these two tables:
OrganismTable (correct) OrganismId (correct) Animal (wrong, should be "AnimalTable") Organism_id (wrong, should be "AnimalId")
I also tried adding:
public class ForeignKeyColumnNameConvention : ForeignKeyConvention { protected override string GetKeyName(Member property, Type type) { if (property == null) return type.Name + "Id"; return property.Name + "Id"; } }
He created these two tables:
OrganismTable (correct) OrganismId (correct) Animal (wrong, should be "AnimalTable") OrganismId (wrong, should be "AnimalId")
I also tried adding:
public class AnimalOverride : IAutoMappingOverride<Animal> { public void Override(AutoMapping<Animal> mapping) { mapping.Table("AnimalTable"); mapping.Id(x => x.Id).Column("AnimalId"); } }
He created the following tables:
OrganismTable (correct) OrganismId (correct) AnimalTable (correct) OrganismId (wrong, should be "AnimalId")
This correctly set the table name for "AnimalTable" (but it requires too much manual input, it would be great if there was an agreement that could get the same result), but it was not possible to set the column name to "AnimalId".
Below is the rest of my code:
class Program { static void Main(string[] args) { ISessionFactory sessionFactory = Fluently.Configure() .Database(MsSqlConfiguration .MsSql2008.ConnectionString(connectionString) .ShowSql()) .Mappings(m => m.AutoMappings.Add( AutoMap.Assemblies(typeof(Organism).Assembly) .Conventions.AddAssembly(typeof(Program).Assembly) .UseOverridesFromAssembly(typeof(Program).Assembly))) .ExposeConfiguration(BuildSchema) .BuildConfiguration() .BuildSessionFactory(); } static void BuildSchema(Configuration cfg) { new SchemaExport(cfg).Create(false, true); } }
Any ideas? Thanks.