使用ForeignKey约束的FluentNHibernate一对一

时间:2010-08-12 19:26:31

标签: c# sql nhibernate fluent-nhibernate

好的我有2个对象和一个外键。

第一个对象是。

    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();
    }
}

第二类是

    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);
    }
}

外键是

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

最后这是生成的查询

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

所以问题就是你看到的,无论出于什么原因,它生成的查询都试图使用外键,尽管将外键连接到OutboundEmailStatusID而不是OutboundEmailID

如果有人知道为什么会这样,或者另一种方法,请告诉我。

这对我来说真的很愚蠢,甚至会发生这种情况?!

1 个答案:

答案 0 :(得分:2)

NHibernate中的one-to-one在设计上是一种连接主键的关系。它是隐式关系,而不是显式关系,因为这两个实体仅通过隐含约定相关联,即如果两个键具有相同的值,则它们是相关联的。请参阅:NHibernate one-to-one

根据您的数据库设计,您实际上有一对多 - 多对一关系结构正在进行,而不是一对一。您的域模型可能会将其表达为一对一,但其基础仍然是一对多;在数据库中你的OutboundEmail可以有很多OutboundEmailStatus,因为没有什么能阻止多行具有相同的外键值。

就个人而言,我将其翻转并将外键放在OuboundEmail上。这样你的OutboundEmail就会有多对一的OutboundEmailStatus。又名。电子邮件具有单一状态,但状态可能与多封电子邮件相关联。