views:

56

answers:

1

Hi,

I'm working on a bug tracking application. There are tickets, and each ticket has an opener user and an assigned user. So, basically, I have two entities, which have two many-to-one relationships with each other. Their schematic is this:

User:

public class User
{
    public virtual int Id { get; protected set; }
    ...

    public virtual IList<Ticket> OpenedTickets { get; set; }
    public virtual IList<Ticket> AssignedTickets { get; set; }
}

Ticket:

public class Ticket
{
    public virtual int Id { get; protected set; }
    ...

    [Required]
    public virtual User OpenerUser { get; set; }
    public virtual User AssignedUser { get; set; }
}

I use FluentNHibernate's auto mapping feature.

The problem is, that no matter whether relationship I set, on the side of the User, both collections always contain the same data. I guess Fluent can't tell which end of which relationship belongs to where.

I googled around but haven't found anything useful.

EDIT 2:

I fould out with the help of Steves that I have to implement my own IHasManyConvention. I also found out that the generated column name in the IHasManyConvention has to match the column name generated by my IReferenceConvention. With a litte tweaking, I got it to work.

I also shockingly found that calling the convenient ForeignKey.EndsWith("Id") method messes up the whole thing.

While Steves's answer in itself doesn't solve the problem, I am very thankful for him pointing me at the right direction.

+2  A: 

Auto mappping generates the same name for their foreign key columns. You can overcome this with custom convention that names the foreign key column by the (mapped) property name:

class HasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(instance.Member.Name);
    }
}

// register convention using:
autoMapping.Conventions.Add(new HasManyConvention());
Steves