【问题标题】:Reverse.KeyColumn("[...]") in Fluent NHibernateFluent NHibernate 中的 Reverse.KeyColumn("[...]")
【发布时间】: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


【解决方案1】:

实际上,User_id 列是由 Fluent NHibernate 中的 SchemaExport() 生成的。我删除它并引用 KeyColumn("Executor_id") 并且它工作正常。

我认为是 FluentNHibernate 的 SchemaExport() 引起的问题。

【讨论】:

    猜你喜欢
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多