1

via fluent nhibernate, I cant use automapping with conventions because it adds extra foreign key to the table for relation. The problem was explained in detail at stackoverflow.com/questions/6091654/fluentnhibernate-automapping-onetomany-relation-using-attribute-and-convention/7867516

But as you can see, it is solved by using attribute. My question is:

I dont want to use attribute on Model Properties . Because we maybe wont use nhibernate in the project in following years. So I dont want to touch Models. Is there a solution for the problem without KeyColumnAttribute.

Thanks

Community
  • 1
  • 1
whitestream
  • 691
  • 1
  • 7
  • 14

1 Answers1

0
class BiDirectionalHasManyConvention : IReferenceConvention, IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        // looks for a Property which references the type containing the collection
        var referenceProperty = instance.ChildType.GetProperties()
            .First(prop => prop.PropertyType == instance.EntityType);

        instance.Key.Column(referenceProperty.Name + "Id");
    }

    // Optional, just to make sure the foreignkey column is propertyname + "Id"
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(instance.Name + "Id");
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • what happens if Address class has another Person type, like `public virtual Person UpdatedBy{ get; set; }` beside Owner property. With IReferenceConvention the name of the property is already described, Why does fluent use IHasManyConvention to rename it. – whitestream Dec 08 '11 at 08:54
  • And in select statement generated by person.Addresses ,it adds two same fields. one comes from reference convention and the other comes from hasmany convention. For Example: select ownerid as first,adressid,ownerid as second from address where ownerid=? – whitestream Dec 08 '11 at 09:02
  • @whitestream mind you, NH is written with the most awkward schemas in mind so it will not assume that it is always a simple bidirectional, but there could be: 1) more than one Reference to the parent 2) the Reference points to another parent 3) the Reference points to an inheritance-hirarchy ... so the easiest way is to take the many-to-one and the one-to-many as totally seperate things, hence selecting the id twice. – Firo Dec 08 '11 at 09:15
  • I think the best thing is to use hbm files with fluent together. if Model is simple, I let fluent do the work. Otherwise I'll use .hbm files for complex model relations. – whitestream Dec 08 '11 at 09:33
  • i use automapping if i have model first, FluentMapping for complex mappings or database first and only if i have to hbm. even complex relations are most of the time simpler in FluentMapping than in hbm (Refactoring easier, less verbose, FluentInterface show better what is possible, many standards are ready to use as Methods like Database configuration and generators, DRY with Conventions ...) – Firo Dec 08 '11 at 09:45