流利的nhibernate manytomany映射

时间:2010-02-21 20:49:02

标签: nhibernate fluent-nhibernate

我正在尝试映射这些实体 消息表字段是Id生成的Id,SentFrom int,sentTo int 其中Sentfrom和sentTo链接到users表 我也有一个链接表 带有userId和MessageId的MessageUser表

    CREATE TABLE [dbo].[MessageUser](
 [MessageId] [int] NOT NULL,
 [UserId] [int] NOT NULL,
 CONSTRAINT [PK_MessageUser] PRIMARY KEY CLUSTERED 
(
 [MessageId] ASC,
 [UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[MessageUser]  WITH CHECK ADD  CONSTRAINT [FK_MessageUser_Messages] FOREIGN KEY([MessageId])
REFERENCES [dbo].[Messages] ([Id])
GO
ALTER TABLE [dbo].[MessageUser] CHECK CONSTRAINT [FK_MessageUser_Messages]
GO
ALTER TABLE [dbo].[MessageUser]  WITH CHECK ADD  CONSTRAINT [FK_MessageUser_Users] FOREIGN KEY([UserId])
REFERENCES [dbo].[Users] ([Id])
GO
ALTER TABLE [dbo].[MessageUser] CHECK CONSTRAINT [FK_MessageUser_Users]
GO

我认为数据库设计还可以,但我无法找出映射实体的最佳方法 我的最终目标是能够使用用户和消息实体获取消息sentTo和消息发送。 任何想法如何实现这一目标。 映射类看起来像

public MessageMap()
        {
            Table("Messages");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Title);
            Map(x => x.Body);
            References(x => x.Recipient).Column("SentTo").Column("Id");//.Cascade.All();
            References(x => x.Sender).Column("SentFrom").Column("Id");//.Cascade.All();
        }

EDIT 这似乎无需使用额外的MessageUser表

  public MessageMap()
        {
            Table("Messages");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Title);
            Map(x => x.Body);

            References(x => x.Recipient).Column("SentTo").ForeignKey("Id").Cascade.All();
            References(x => x.Sender).Column("SentFrom").ForeignKey("Id").Cascade.All();
        }

由于

0 个答案:

没有答案