Let's say I have these models:
public class Person { public virtual int Id { get; private set; } public virtual ICollection<Address> Addresses { get; private set; } } public class Address { public virtual int Id { get; private set; } public virtual Person Person { get; set; } }
I want FluentNHibernate to create the following tables:
Person PersonId Address AddressId PersonId
This can be easily achieved using free mapping:
public class PersonMapping : ClassMap<Person> { public PersonMapping() { Id(x => x.Id).Column("PersonId"); HasMany(x => x.Addresses).KeyColumn("PersonId"); } } public class AddressMapping : ClassMap<Address> { public AddressMapping() { Id(x => x.Id).Column("AddressId"); References(x => x.Person).Column("PersonId"); } }
I want to get the same result using automatic matching. I tried the following conventions:
class PrimaryKeyNameConvention : IIdConvention { public void Apply(IIdentityInstance instance) { instance.Column(instance.EntityType.Name + "Id"); } } class ReferenceNameConvention : IReferenceConvention { public void Apply(IManyToOneInstance instance) { instance.Column(string.Format("{0}Id", instance.Name)); } }
But I sadly found that he created the following tables:
Person PersonId Address AddressId PersonId Person_id // this column should not exist
Below is the rest of my code:
ISessionFactory sessionFactory = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)) .Mappings(m => m.AutoMappings.Add(AutoMap.Assemblies(typeof(Person).Assembly) .Conventions.Add(typeof(PrimaryKeyNameConvention)) .Conventions.Add(typeof(ReferenceNameConvention)))
Any ideas? Thanks.
user593358
source share