I have just started using castle active record and i am having some problems using a foreign key as the discriminator column. I get the error "Invalid index 2 for this SqlParameterCollection with Count=2."
The tables i am using are:-
TABLE MemberRoles
[Id] [int] IDENTITY NOT NULL,
[Name] [varchar] NOT NULL
TABLE Members
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[MemberRoleId] [int] NOT NULL
My classes are :-
[ActiveRecord(DiscriminatorValue = "3")]
public class Offical : Member<Offical>
{
}
[ActiveRecord(DiscriminatorValue = "2")]
public class Player : Member<Player>
{
}
[ActiveRecord(Table = "Members", DiscriminatorColumn = "MemberRoleId", DiscriminatorType = "integer", DiscriminatorValue = "0")]
public abstract class Member<T> : DomainObjectBase<T> where T : Member<T>
{
[BelongsTo("MemberRoleId")]
public virtual MemberRole MemberRole { get; set; }
[Property]
public virtual string FirstName { get; set; }
[Property]
public virtual string LastName { get; set; }
}
public abstract partial class DomainObjectBase<T> : ActiveRecordBase<T> where T : DomainObjectBase<T>
{
[PrimaryKey]
public virtual int Id { get; private set; }
}
I want to load a Member as either a player or an offical based on their member role, the problem seems to be that the nhibernate mapping that is generated contains the column MemberRoleId twice, once as the Discriminator column and once as a many-to-one column for the MemberRole property.
<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2">
<class name="DomainModel.Member`1, DomainModel" table="Members" discriminator-value="0">
<id name="Id" access="property" column="Id" type="Int32" unsaved-value="0">
<generator class="native">
</generator>
</id>
<discriminator column="MemberRoleId" type="integer" />
<property name="FirstName" access="property" type="String">
<column name="FirstName"/>
</property>
<property name="LastName" access="property" type="String">
<column name="LastName"/>
</property>
<many-to-one name="MemberRole" access="property" class="DomainModel.MemberRole, DomainModel" column="MemberRoleId" lazy="proxy" />
<subclass name="DomainModel.Player, DomainModel" discriminator-value="2">
</subclass>
<subclass name="DomainModel.Offical, DomainModel" discriminator-value="3">
</subclass>
</class>
</hibernate-mapping>
Any help on how to implement this sort of relationship with castle activerecord would be welcome.