Dear All,
I have a problem when saving a object to the database. I'm using C# and Sql Server.
My domain objects are:
Code:
public class UserSpecialization : PersonSpecialization, IUserSpecialization
{
public static readonly string SpecializationNameValue = "User";
public UserSpecialization()
{
Roles = new Collection<IRole>();
}
public virtual IEnumerable<IRole> Roles
{
get;
set;
}
}
public abstract class PersonSpecialization : DomainObjectOfInt32, IPersonSpecialization
{
public virtual IPerson Person { get; set; }
public virtual string SpecializationName { get; protected set; }
}
public class Person : DomainObjectOfInt32, IPerson, IAuditable
{
public Person()
{
Specializations = new List<IPersonSpecialization>();
}
public virtual IList<IPersonSpecialization> Specializations { get; protected set; }
}
public abstract class Role : DomainObjectOfInt32, IRole, IAuditable
{
public Role(string roleName)
: this()
{
Name = roleName;
}
protected Role()
{
AssignedUsers = new Collection<IUserSpecialization>();
}
public virtual ICollection<IUserSpecialization> AssignedUsers
{
get;
protected set; }
}
My mapping is as follow:
Code:
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id).GeneratedBy.Identity();
HasMany<PersonSpecialization>(c => c.Specializations)
.Cascade.AllDeleteOrphan();
}
}
public class RoleMap : ClassMap<Role>
{
public RoleMap()
{
Id(x => x.Id).GeneratedBy.Identity();
HasManyToMany<UserSpecialization>(x => x.AssignedUsers).Table("UserRole");
DiscriminateSubClassesOnColumn("Type");
}
}
public class PersonSpecializationMap : ClassMap<PersonSpecialization>
{
public PersonSpecializationMap()
{
Table("PersonSpecialization");
Id(c => c.Id).GeneratedBy.Identity();
DiscriminateSubClassesOnColumn<string>("SpecializationName");
Map(x => x.SpecializationName).ReadOnly();
References<Person>(c => c.Person);
}
}
public class UserSpecializationMap : SubclassMap<UserSpecialization>
{
public UserSpecializationMap()
{
Table("User");
DiscriminatorValue(UserSpecialization.SpecializationNameValue);
Join("[User]", joined =>
{
joined.Map(c => c.UserName, "Username");
joined.HasManyToMany<Role>(x => x.Roles)
.Table("UserRole");
});
}
}
Now when i select a Person, i get also all his roles in Roles collection. Also when saving a Role, all linked Persons are nicely saved. But when i save a Person, with roles linked to it, i'm getting this exception:
Nhibernate.PropertyAccessException:Exception occurred getter of DO.DomainObjectOfInt32.Id. InnerException:Object does not match target type.
How can i solve this issue?