Hi,
I have problems inserting data with a many-to many relation as follows
Code:
table Company
{
int SysId
...
}
Premise
{
int SysId
...
}
CompanyPremise
{
int CompanyId,
int PremiseId
/*Both form composite primary key*/
}
Entity calses
public class Premise
{
public Premise( )
public virtual IList<CompanyPremise> CompanYPremises{ get; set; }
....
}
public PremiseMap()
{
Table("Premise");
LazyLoad();
Id(x => x.SysId).GeneratedBy.Identity().Column("SysId");
.....
HasMany(x => x.CompanyPremises);
}
public class Company
{
public Company()
{
}
public virtual int SysId { get; set; }
public virtual IList<CompanyPremise> CompanyPremises { get; set; }
...
}
public class CompanyPremise
{
public CompanyPremise() { }
public virtual int CompanyId { get; set; }
public virtual int PremiseId { get; set; }
public virtual Company Company { get; set; }
public virtual Premise Premise { get; set; }
}
public class CompanyPremiseMap : ClassMap<CompanyPremise>
{
public CompanyPremiseMap()
{
Table("CompanyPremise");
LazyLoad();
CompositeId().KeyProperty(x => x.CompanyId).KeyProperty(x => x.PremiseId);
References(x => x.Company).Column("CompanyId");
References(x => x.Premise).Column("PremiseId");
}
}
public CompanyMap()
{
Table("Company");
LazyLoad();
Id(x => x.SysId).GeneratedBy.Identity().Column("SysId");
HasMany(x => x.CompanyPremises);
}
I am trying to insert into CompanyPremise table like this
Code:
var trans =session.BeginTransaction();
var cm = session.Get<Company>( companyId );
var pm = session.Get<Premise>( premiseId );
var cp = new CompanyPremise
{
Company = cm,
Premise = pm
};
session.SaveOrUpdate( cp );
transaction.Commit();
This does not work. Everything is fine utill Commit( );
When it throws an exception:
Invalid index 2 for this SqlParameterCollection with Count=2
I am pulling my hair out. Entire day and I have no idea what I am doing wrong
Please help!!