I am working with a legacy database, where I have two tables (Staff, 
Rights) and a joint table (StaffRights). I want to map the one to many 
relationship/join Staff and staffrights. I have used half day trying to 
figure this out

With the current code, I get the following exception:
    
    Exception thrown: 'FluentNHibernate.Cfg.FluentConfigurationException' 
in 
    FluentNHibernate.dll

    Additional information: An invalid or incomplete configuration was used 
    while creating a SessionFactory. Check PotentialReasons collection, and 
    InnerException for more detail.

    Inner exception:System.ArgumentException: The column 'StaffID' has 
already been added in this SQL builder Parameter name: columnName at 
NHibernate.SqlCommand.SqlInsertBuilder.AddColumnWithValueOrType(String 
    columnName, Object valueOrType) at 
NHibernate.SqlCommand.SqlInsertBuilder.AddColumns(String[] 
    columnNames, Boolean[] insertable, IType propertyType)
       at 
NHibernate.Persister.Entity.AbstractEntityPersister.GenerateInsertString(Boolean
 
identityInsert, Boolean[] includeProperty, Int32 j)}

    Table1 --> Staff
    StaffID: PK
    Name

    Table2 --> StaffRights
    SRID: PK
    StaffID:FK
    RightID:FK
    CreatedTS

    Table3 -->Rights
    RightID: PK
    RightName


    public class Staff
    {
        public virtual int Id { get; protected set; }
        public virtual string Name { get; set; }

        public virtual IList<StaffRights> RightsAssociation { get; set; }
        public Staff()
        {
            RightsAssociation = new List<StaffRights>();
        }
    }

    public class StaffRights
    {
        public virtual int Id { get; protected set; }
        public virtual int StaffID { get; set; }
        public virtual int RightID { get; set; }
        public virtual Staff Staff { get; set; }
    }

    public class StaffMap : ClassMap<Staff>
    {
        public StaffMap()
        {
            Id(x => x.Id).Column("StaffID").GeneratedBy.Native();
            Map(x => x.Name);
            HasMany(x => x.RightsAssociation).KeyColumn("StaffID");
            Table("Staff");
        }
    }

    public class StaffRightsMap: ClassMap<StaffRights>
    {
        public StaffRightsMap()
        {
            Id(x => x.Id).Column("SRID").GeneratedBy.Native();
            Map(x => x.StaffID);
            Map(x => x.RightID);
            References(x => x.Staff).Column("StaffID");
            Table("StaffRights");
        }
    }

    _sessionFactory = Fluently.Configure()
                    
 .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connStr))
    .Mappings(m =>
    {
        m.FluentMappings.AddFromAssemblyOf<Staff>();

        m.FluentMappings.AddFromAssemblyOf<StaffRights>();
    })
    .BuildSessionFactory();

I have removed the 

    References(x => x.Staff).Column("StaffID");
And I am able to get some results from:

    var staff = session.Get<Staff>(2);
    var rights = staff.RightsAssociation;
    var f = rights.First();

However, as I understand, the mapping shall be bidirectional. What have I 
misunderstood?

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fluent-nhibernate+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/fluent-nhibernate/c660a85c-d61e-42bd-af98-41625f99478f%40googlegroups.com.

Reply via email to