Setting FK Constraint Name in Many-to-Many Convention

Original Post David Archer's Avatar

David Archer

24 Feb, 2010 05:23 AM via web

I'm trying to setup a convention for readable FK constraint names for my automapped models. I've been able to get it working for 1:M, M:1, and JoinedSubClass but I'm having trouble with M:M.

I can do it in my classmap or automappingoverride using something like:

        mapping.HasManyToMany(x => x.Collection)
            .ForeignKeyConstraintNames("FK_Parent", "FK_Child");

Basically, how can I set the equivalent values in a IHasManyToManyConvention?

I implement the IHasManyToManyConvention and I can set the Parent FK Constraint Name using:

    public void Apply(IManyToManyCollectionInstance instance)
    {
        instance.Key.ForeignKey("parentKeyName");
    }

but I can't figure out how to set the Child FK Constraint Name.

  1. Support Staff 2 Posted by James Gregory on 24 Feb, 2010 09:43 AM

    James Gregory's Avatar

    You should be able to do this:

    instance.Relationship.ForeignKey("childKeyName");
    

    Unfortunately, it seems we've missed that method off the conventions. I've created a ticket (#129) for this, and we'll get to it asap. We're currently two men down though, so we may not be able to get around to it until next week.

    If you fancy taking this on yourself, I can point you to the right files.

  2. 3 Posted by David Archer on 24 Feb, 2010 02:50 PM

    David Archer's Avatar

    James,

    Point away. :) I'll see what I can do.

    -- David

  3. Support Staff 4 Posted by James Gregory on 24 Feb, 2010 03:02 PM

    James Gregory's Avatar

    Great, you're my favourite kind of user. :)

    Firstly, if you take a look at Conventions/Instances/KeyInstance.cs you'll see that there's a ForeignKey method defined (exposed through the IKeyInstance interface). This is what you'll need to duplicate.

    The class that will need modifying is the Conventions/Instances/ManyToManyInstance.cs, and you should be able to use the exact same code from KeyInstance to create the ForeignKey method; you'll also need to expose the method through the IManyToManyInstance interface.

    That should be all that you'll need to do. Let me know if that doesn't work.

  4. 5 Posted by David Archer on 24 Feb, 2010 06:32 PM

    David Archer's Avatar

    Just sent you a pull request on github for these changes plus unit tests.

  5. Support Staff 6 Posted by Paul Batum on 28 Feb, 2010 01:27 PM

    Paul Batum's Avatar

    Pulled into my repository, it'll go into the trunk with the next batch. Thanks David.

  6. Support Staff 7 Posted by James Gregory on 01 May, 2010 12:56 PM

    James Gregory's Avatar

    Merged into master.

  7. James Gregory resolved this discussion on 01 May, 2010 12:56 PM.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

05 Jul, 2010 10:29 PM
05 Jul, 2010 12:45 PM
05 Jul, 2010 12:42 PM
05 Jul, 2010 12:17 PM
05 Jul, 2010 12:12 PM

 

03 Jul, 2010 12:26 AM
02 Jul, 2010 02:17 PM
02 Jul, 2010 08:18 AM
02 Jul, 2010 12:20 AM
01 Jul, 2010 10:14 PM