views:

19

answers:

1

Ok I have 2 objects and a foreign key.

The first object is.

    public class OutboundEmailMap : ClassMap<OutboundEmail>
{
    public OutboundEmailMap()
    {
        Table("OutboundEmail");

        Id(x => x.Id, "OutboundEmailId")
            .UnsavedValue(0)
            .GeneratedBy.Identity();

        Map(x => x.OutboundEmailGuid);
        Map(x => x.FromAccountName);
        Map(x => x.ToAccountName);
        Map(x => x.FromContactFirstName);
        Map(x => x.FromContactLastName);
        Map(x => x.ToContactFirstName);
        Map(x => x.ToContactLastName);
        Map(x => x.FromEmailAddress);
        Map(x => x.ToEmailAddress);
        Map(x => x.EmailTemplateID);
        Map(x => x.SentDate);
        Map(x => x.Subject);
        Map(x => x.XMLTokenDictionary);
        Map(x => x.IsFax);
        Map(x => x.TransmittalId);

        //References<Transmittal>(x => x.Transmittal)
        //    .Column("TransmittalID")
        //    .LazyLoad()
        //    .Cascade.None();

        HasOne<OutboundEmailStatus>(x => x.Status)
            .ForeignKey("FK_OutboundEmailStatus_OutboundEmail")
            .Cascade.None();
    }
}

The 2nd Class is

    public class OutboundEmailStatusMap : ClassMap<OutboundEmailStatus>
{
    public OutboundEmailStatusMap()
    {
        Table("OutboundEmailStatus");

        Id(x => x.Id, "OutboundEmailStatusID")
            .UnsavedValue(0)
            .GeneratedBy.Identity();

        References(x => x.OutboundEmail, "OutboundemailID");
        Map(x => x.EmailStatus, "EmailStatusID");
        Map(x => x.EmailStatusDate);
    }
}

And the Foreign Key is

USE [Mail]
GO

ALTER TABLE [dbo].[OutboundEmailStatus]  WITH CHECK ADD  CONSTRAINT   [FK_OutboundEmailStatus_OutboundEmail] FOREIGN KEY([OutboundEmailID])
REFERENCES [dbo].[OutboundEmail] ([OutboundEmailID])
GO

ALTER TABLE [dbo].[OutboundEmailStatus] CHECK CONSTRAINT [FK_OutboundEmailStatus_OutboundEmail]
GO

And Lastly this is the query that is generated

SELECT   top 20 this_.OutboundEmailId              as Outbound1_1_1_,
            this_.OutboundEmailGuid            as Outbound2_1_1_,
            this_.FromAccountName              as FromAcco3_1_1_,
            this_.ToAccountName                as ToAccoun4_1_1_,
            this_.FromContactFirstName         as FromCont5_1_1_,
            this_.FromContactLastName          as FromCont6_1_1_,
            this_.ToContactFirstName           as ToContac7_1_1_,
            this_.ToContactLastName            as ToContac8_1_1_,
            this_.FromEmailAddress             as FromEmai9_1_1_,
            this_.ToEmailAddress               as ToEmail10_1_1_,
            this_.EmailTemplateID              as EmailTe11_1_1_,
            this_.SentDate                     as SentDate1_1_,
            this_.Subject                      as Subject1_1_,
            this_.XMLTokenDictionary           as XMLToke14_1_1_,
            this_.IsFax                        as IsFax1_1_,
            this_.TransmittalId                as Transmi16_1_1_,
            outboundem2_.OutboundEmailStatusID as Outbound1_7_0_,
            outboundem2_.EmailStatusID         as EmailSta2_7_0_,
            outboundem2_.EmailStatusDate       as EmailSta3_7_0_,
            outboundem2_.OutboundemailID       as Outbound4_7_0_
FROM     OutboundEmail this_
     left outer join OutboundEmailStatus outboundem2_
       on this_.OutboundEmailId = outboundem2_.OutboundEmailStatusID
WHERE    this_.TransmittalId = '7789322e-acd6-4cb8-9c43-5bdaec52aa8a' /* @p0 */
ORDER BY this_.ToAccountName asc

So the issue is as you see, for whatever reason the query it generates tries to use the foreign key, though connecting the foreign key to OutboundEmailStatusID instead of OutboundEmailID

If anyone knows why this might happen, or an alternative approach, please let me know.

It seems really silly to me that this even happens?!

A: 

A one-to-one in NHibernate is, by design, a relationship that joins over the primary key. It's an implicit relationship, rather than explicit, in that the two entities are related only through an implied convention that if the two keys have the same value they're associated. See: NHibernate one-to-one.

Judging by your database design, you actually have a one-to-many - many-to-one relationship structure going on, not a one-to-one. Your domain model may express it as a one-to-one, but underlying it's still a one-to-many; in the database your OutboundEmail can have many OutboundEmailStatus's because there's nothing stopping multiple rows having the same foreign-key value.

Personally, I'd flip it around and put the foreign-key on the OuboundEmail. That way your OutboundEmail would have a many-to-one to OutboundEmailStatus. aka. an email has a single status, but a status could be associated with multiple emails.

James Gregory
Yeah we were talking about putting the key on the OutboundEmail table. Thanks a lot for your input and it makes a lot of sense.
Matt Braunwart
And I guess I don't have enough reputation to vote you up :(
Matt Braunwart