Fluent NHibernate Mapping help
I am getting the following error:
Invalid index 3 for this OracleParameterCollection with Count=3
when i am attempting to insert a new column into a table.
my entity class is:
public class Operator
{
public Operator()
{
this.OperatorMemberships = new List<OperatorMembership>();
}
public virtual int Id { get; set; }
public virtual int OperatorMembershipId { get; set; }
public virtual int SchemeId { get; set; }
public virtual string Description { get; set; }
public virtual IList<OperatorMembership> OperatorMemberships { get; set; }
}
public class OperatorMembership
{
public virtual int Id { get; set; }
public virtual int OperatorId { get; set; } // delete
public virtual int UserRoleId { get; set; } // delete
public virtual UserRole UserRole { get; set; }
public virtual Operator Operator { get; set; }
}
My mappings are:
public OperatorMap()
{
Table("OPERATOR");
Id(x => x.Id, "OPERATOR_ID").GeneratedBy.Identity();
Map(x => x.SchemeId, "SCHEME_ID");
Map(x => x.Description, "DESC开发者_如何学CRIPTION");
//HasMany(x => x.OperatorMemberships).Inverse().AsBag().Cascade.AllDeleteOrphan();
HasMany(x => x.OperatorMemberships).Inverse().AsBag();
//References(x => x.SchemeServices, "OPERATOR_ID");
}
}
public class OperatorMembershipMap : ClassMap<OperatorMembership>
{
public OperatorMembershipMap()
{
Table("USER_OPERATOR_MEMBERSHIP");
Id(x => x.Id, "USER_OPERATOR_MEMBERSHIP_ID").GeneratedBy.Identity();
Map(x => x.OperatorId, "OPERATOR_ID");
Map(x => x.UserRoleId, "USER_ROLE_ID");
References(x => x.Operator).Column("OPERATOR_ID"); ;
References(x => x.UserRole).Column("USER_ROLE_ID");
}
}
the code to insert the new row is:
public int AddOperatorMembership(int operatorId, int userRoleId)
{
OperatorMembership op = new OperatorMembership();
op.OperatorId = operatorId;
op.UserRoleId = userRoleId;
_provider.AddEntity(op);
return op.Id;
}
and my database table is:
USER_OPERATOR_MEMBERSHIP_ID -- OPERATOR_ID -- USER_ROLE_ID
any help would be good, i am sure there are some very clever fluent nhibernate developers out there who can spot the error straight away.
many thanks.
Randolf.
You're mapping the referenced entities AND the foreign key :
public OperatorMembershipMap()
{
Table("USER_OPERATOR_MEMBERSHIP");
Id(x => x.Id, "USER_OPERATOR_MEMBERSHIP_ID").GeneratedBy.Identity();
// Map(x => x.OperatorId, "OPERATOR_ID");
// Map(x => x.UserRoleId, "USER_ROLE_ID");
References(x => x.Operator).Column("OPERATOR_ID"); ;
References(x => x.UserRole).Column("USER_ROLE_ID");
}
You can also remove the foreign keys from your entity :
public class OperatorMembership
{
public virtual int Id { get; set; }
// public virtual int OperatorId { get; set; } // delete
// public virtual int UserRoleId { get; set; } // delete
public virtual UserRole UserRole { get; set; }
public virtual Operator Operator { get; set; }
}
I too came across the same issue and ended up googling for half a day but no success. The above answer given by Mathieu seemd relevant and i tried the same but did not work for me. Then i tried with removing references and keeping the properties intact, it worked for me and hope will work for you as well.
Table("USER_OPERATOR_MEMBERSHIP");
Id(x => x.Id, "USER_OPERATOR_MEMBERSHIP_ID").GeneratedBy.Identity();
Map(x => x.OperatorId, "OPERATOR_ID");
Map(x => x.UserRoleId, "USER_ROLE_ID");
//References(x => x.Operator).Column("OPERATOR_ID"); ;
//References(x => x.UserRole).Column("USER_ROLE_ID");
Remove the same from class as well
public class OperatorMembership
{
public virtual int Id { get; set; }
public virtual int OperatorId { get; set; } // delete
public virtual int UserRoleId { get; set; } // delete
//public virtual UserRole UserRole { get; set; }
//public virtual Operator Operator { get; set; }
}
精彩评论