我有以下域对象:
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; }
}
我的映射如下:
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);
}
}
现在当我选择一个人时,我也会在Roles系列中获得他所有的角色。此外,在保存角色时,可以很好地保存所有链接的人员。 但是当我保存一个Person时,该Person的链接角色不会保存。
我该如何解决这个问题?
UserSpecialization的映射:
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).Inverse()
.Table("UserRole");
});
}
}
答案 0 :(得分:0)
你错过了
public class UserSpecializationMap : SubclassMap<UserSpecialization>
{
public UserSpecializationMap()
{
Table("PersonSpecialization");
Id(c => c.Id).GeneratedBy.Identity();
DiscriminatorValue("UserSpecialization");
HasManyToMany(x => x.Roles).Cascade.All()...;
}
}
btw:我会为specializationName使用polymorph属性,然后你不需要专门初始化属性,你不能忘记它在一个dervied类中,它不会在对象中占用空间。
// base class
public abstract string SpecializationName { get; }
// dereived class
public override string SpecializationName { get { return "User"; } }