【发布时间】:2013-12-26 17:48:23
【问题描述】:
关键是 KeyColumn 在 BASE 表中。从下面的代码可以看出User和Follow之间是一对多的关系。
public class Activity
{
public virtual User Executor { get; set; }
//probably I should set some kind of Polymorphism here?
}
public class Follow : Activity
{
public virtual User Followee { get; set; }
}
public class User
{
public virtual IList<Follow> Follows { get; set; }
}
所以我将它们映射如下:
public class ActivityMap : EntityMap<Activity>
{
public ActivityMap()
{
References<User>(m => m.Executor);
}
}
public class FollowMap : SubclassMap<Follow>
{
public FollowMap()
{
References(m => m.Followee);
}
}
public class UserMap : EntityMap<User>
{
public UserMap()
{
HasMany(m => m.Follows)
.Inverse();
//.KeyColumn("Executor_id");
}
}
请注意 KeyColumn() 映射。 “Executor_id”不在表 tb_Follow 中,而是在表 tb_Activity(用于子类策略)中。如果像上面这样的代码,会在tb_Follow中生成一个新的列“Executor_id”,我认为是重复的。
现在我如何引用 tb_Activity 中的“Executor_id”列?
PS:表结构。
tb_Follow
+-------------+-------------+---------+
+ Activity_id + Followee_id + User_id +
+-------------+-------------+---------+
+ 1 + 7 + NULL +
+-------------+-------------+---------+
tb_Activity
+-------------+-------------+---------+
+ id + Executor_id + Type +
+-------------+-------------+---------+
+ 1 + 6 + Follow +
+-------------+-------------+---------+
所以是 User(6) -- 关注 --> User(7)
在我的分辨率中,User_id 应该是 6,与 Executor_id 相同(我认为是重复的)
【问题讨论】:
-
是不是和多态性有关的东西造成的?
-
我想说:如果你想同时存在两个属性:Executor 和 Followee - 那么需要 2 列。取决于它们在实体(C#)中的放置位置......它们的表表示形式必须相同。如果您只想要一个用户...再次,我们只需要决定在哪个级别...或者我错过了什么?
-
是的,Executor 和 Followee 都在这里。但是 Executor 是 Base 类型的 Activity。所以在上表中,如你所见,User_id 与 Executor_id 重复
标签: nhibernate fluent-nhibernate reverse