views:

611

answers:

1

I'm using FluentNHibernate and have done a many-to-many mapping but when I try to save my entity I get the following error:

NHibernate.Exceptions.GenericADOException: NHibernate.Exceptions.GenericADOException
: could not insert collection: [Test.Entities.Recipient.Groups#b6815d34-f436-4142-9b8e-1bfcbf25509e][SQL: SQL not available]
---- System.Data.SQLite.SQLiteException : Abort due to constraint violation
columns GroupId, idx are not unique

Here is my mapping:

public class GroupMap : ClassMap<Group>
{
    public GroupMap()
    {
        Id(x => x.Id).GeneratedBy.Guid();
        Map(x => x.Name);
        Map(x => x.SenderName);
        Map(x => x.Created);
        HasManyToMany(x => x.Recipients)
            .AsList()
            .WithTableName("groups_recipients")
            .WithParentKeyColumn("GroupId")
            .WithChildKeyColumn("RecipientId")
            .LazyLoad()
            .Cascade.AllDeleteOrphan();
    }
}

public class RecipientMap : ClassMap<Recipient>
{
    public RecipientMap()
    {
        Id(x => x.Id).GeneratedBy.Guid();
        Map(x => x.Firstname);
        Map(x => x.Lastname);
        Map(x => x.Phone);
        Map(x => x.Email);
        HasManyToMany(x => x.Groups)
            .AsList()
            .WithTableName("groups_recipients")
            .WithParentKeyColumn("RecipientId")
            .WithChildKeyColumn("GroupId")
            .LazyLoad().Cascade.None();
    }
}

The problem seems to have something to do with the relationship tables id but I can't figure out how to solve it.

Cheers, nandarya

+1  A: 

Using AsList() was not a good idea. Should be AsBag(). And everything seems to work.

nandarya
Seems to happen with certain One-to-many mappings as well. Using AsBag() solves the problem
chakrit

related questions