views:

43

answers:

0

Hello everybody.

I'm having issue when I use Fluent nHibernate for mapping entities with my SQL database.

To understand the problem, here is an example. This example modelizes via SQL tables a mail box, that allows users to put their mail in one or many folder (like the label system in gmail).

// contains every users
USER
(
    Id (bigint) - PRIMARY KEY
    UserName (varchar)
)

// contains every mail received
MAIL
(
    Id (bigint) - PRIMARY KEY
    Title (nvarchar)
    Content (nvarchar)
)

// it symbolizes links between users and  their mails
MAILBOX
(
    Id (bigint) - PRIMARY KEY
    UserId (bigint) - FOREIN KEY => USER(Id)
    MailId (bigint) - FOREIGN KEY => MAIL(Id)
    WhenAdded (datetime)
)

// contains every user folder created
FOLDER
(
    Id (bigint)
    Label (nvarchar)
    UserId (bigint)

    CONSTRAINT (Label+UserId must be unique)
)

// contains the links between mails and folders
FOLDER_MAIL
(
    Id (bigint) - PRIMARY KEY
    MailId  (bigint) - FOREIGN KEY => MAIL(Id)
    FolderlId (bigint) - FOREIGN KEY => FOLDER(Id)

    CONSTRAINT (MailId & FolderId must be unique)
)

Here is the MailBox mapping (Fluent) :

internal class MailBoxMap : ClassMap<DAL.Entities.MailBox>
{
    public MailBoxMap()
    {
        Table("MAILBOX");
        Id(x => x.Id, "Id")
            .GeneratedBy.Native();
        Map(x => x.WhenAdded, "WhenAdded");
        References(x => x.User)
            .Not.Nullable();
        References(x => x.Mail)
            .Not.Nullable();

        HasManyToMany<DAL.Entities.Folder>(x => x.Folders)
            .Table("FOLDER_MAIL")
            .ParentKeyColumn("MailId")
            .ChildKeyColumn("FolderId")
            .AsSet()
            .LazyLoad()
            .Not.Inverse();
    }
}

If I disable the HasManyToMany relation, the MailBoxMap works fine. User and Mail are correctly loaded.

If I enable the HasManyToMany relation, I get an error when nHibernate try to load Fluent mapping :

Loading :

ISessionFactory factory = Fluently.Configure().Database(
    MsSqlConfiguration.MsSql2005.ConnectionString(dsn).ProxyFactoryFactory(
            typeof(ProxyFactoryFactory).AssemblyQualifiedName)).Mappings(
                m => m.FluentMappings.Add<MailBoxMap>()).BuildSessionFactory();

Error :

"Foreign key (FKAFAF00B62BD88AC4:FOLDER_MAIL [MailBox_id])) must have same number of columns as the referenced primary key (MAILBOX [UserId, MailId])"

I have tried a lot of code changment, but impossible for me to find the solution.

Can you please help me ?

Thanks a lot,

Sincerely,

related questions