在尝试映射我的域时,我有多对任何映射和继承冲突:
public class Member {
public virtual int Id { get; set; }
// many:
public virtual IList<IssuedToken> IssuedTokens { get; set; }
}
public abstract class IssuedToken {
public virtual int Id { get; set; }
// any:
public virtual Member Member { get; set; }
}
public class IssuedRequestToken : IssuedToken { }
public class IssuedAccessToken : IssuedToken { }
我像这样映射继承:
public class IssuedTokenMap : ClassMapping<IssuedToken> {
public IssuedTokenMap() {
Discriminator(t => {
t.Force(true);
t.Insert(true);
t.Length(32);
t.NotNullable(true);
t.Type(NHibernateUtil.String);
t.Column("Discriminator");
});
}
}
public class IssuedAccessTokenMap : SubclassMapping<IssuedAccessToken> {
public IssuedAccessTokenMap() {
DiscriminatorValue("IssuedAccessToken");
}
}
public class IssuedRequestTokenMap : SubclassMapping<IssuedRequestToken> {
public IssuedRequestTokenMap() {
DiscriminatorValue("IssuedRequestToken");
}
}
现在我要映射IssuedToken
(很多)和Member
(任意)关系,但我不知道怎么做!你可以帮助我理解和映射这种关系吗?
注意:我正在使用代码进行NHibernate 3.2
映射 - 不是NHibernateFluent API,也不是XML 。
答案 0 :(得分:1)
尝试:
public class MemberMap : ClassMapping<Member> {
public MemberMap() {
// add a bag -or set.
Bag(t => t.IssuedTokens,
bag => {
bag.Inverse(true);
bag.Key(k => k.Column(col => col.Name("MemberId")));
},
c => c.ManyToAny<int>(m => {
m.Columns(id => {
id.Name("MemberId");
id.NotNullable(true);
}, cls => {
cls.Name("Discriminator");
cls.NotNullable(true);
});
m.IdType<int>();
m.MetaType<string>();
m.MetaValue("IssuedAccessToken", typeof(IssuedAccessToken));
m.MetaValue("IssuedRequestToken", typeof(IssuedRequestToken));
}));
}
}
和
public class IssuedTokenMap : ClassMapping<IssuedToken> {
public IssuedTokenMap() {
Discriminator(t => {
t.Force(true);
t.Insert(true);
t.Length(32);
t.NotNullable(true);
t.Type(NHibernateUtil.String);
t.Column("Discriminator");
});
// add a many-to-one
ManyToOne(t => t.Member, t => {
t.Column("MemberId");
t.NotNullable(true);
});
}
}