AutoMap One-Way Many To Many

richard's Avatar

richard

07 Jun, 2010 09:27 PM via web

Hi,
Sorry if this has been covered, or I'm missing something obvious. Couldn't find anything.

I understand how to AutoMap a ManyToMany relationship. E.g. Roles contain Operations and
Operations can belong to many Roles.

What I can't figure out is how to keep that ManyToMany realtionship, but only allow navigation from
the Roles Class to the Operations. So, Roles has an IList property, but Operation has
no equivalent.

The AutoMap obviously thinks it's a one to many relationship, and doesn't link them correctly.
I've tried using an override but can't get it to work.

Is there an easy way to set up a one-way many to many relationship?

-Rd

  1. 2 Posted by richard on 07 Sep, 2010 04:09 PM

    richard's Avatar

    Sorry to bump this, but It still has me stumped. Does anyone have any idea whether it's possible to AutoMap two classes that have a many to many relationship in the database, but where I only want to allow navigation in one direction between the classes?

    -Rd

  2. Support Staff 3 Posted by Paul Batum on 26 Sep, 2010 04:13 PM

    Paul Batum's Avatar

    You'll have to provide an automapping override.
    http://wiki.fluentnhibernate.org/Auto_mapping#Overrides

    <http://wiki.fluentnhibernate.org/Auto_mapping#Overrides>

    public class PersonMappingOverride
      : IAutoMappingOverride<Person>{
      public void Override(AutoMapping<Person> mapping)
      { mapping.HasManyToMany(x => x.Roles)
               .Cascade.All()
               // etc
      }}

    On Tue, Sep 7, 2010 at 11:11 AM, richard <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  3. 4 Posted by richard on 27 Sep, 2010 10:07 PM

    richard's Avatar

    Thanks Paul.

    I spent ages trying to get this to work and I did try using an automapping override, but I couldn't get it working.
    When I tried it again following your post I got it working. Must be something about coming at it fresh.

    One thing... If Role and Operation both map to each other Many to Many FluentNHibernate expects the link table to be called OperationsToRoles (note plurals).

    When I remove the

        public virtual IList<Role> Roles { get; set; }

    property from Operation and add a mapping override:

    public class RoleMappingOverride : IAutoMappingOverride
    {

    public void Override(AutoMapping<Role> mapping)
    {
        mapping.HasManyToMany(x => x.Operations);
    }

    }

    The expected table name is now OperationToRole (note singular).

    That seems odd to me.

    -Richard

  4. Support Staff 5 Posted by Paul Batum on 28 Sep, 2010 05:00 PM

    Paul Batum's Avatar

    Yeah thats not ideal. We have a few inconsistencies like that, but we have
    to be careful because changing it to make it consistent breaks existing
    schemas. We should probably do a sweep for these before we publish a 2.0
    release.

    On Mon, Sep 27, 2010 at 5:10 PM, richard <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  5. Support Staff 6 Posted by James Gregory on 24 Oct, 2010 10:13 AM

    James Gregory's Avatar

    Definitely. 2.0 is going to be a breaking changes release, so we'll go nuts
    on things like this.

    On Tue, Sep 28, 2010 at 6:03 PM, Paul Batum <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    Five times two is what?