0
votes

The key point is that the KeyColumn is at the BASE table. You can see that there is a one-to-many relationship between User and Follow from the following code.

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; }
}

so I mapped them as the following:

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");
    }
}

Please notice the KeyColumn() map. "Executor_id" is not in table tb_Follow but in table tb_Activity(for subclass strategy). If code like the above, a new column "Executor_id" will be generated in tb_Follow, which I think duplicated.

Now how can I refer to the "Executor_id" column in tb_Activity?

PS: the table structure.

tb_Follow
    +-------------+-------------+---------+
    + Activity_id + Followee_id + User_id +
    +-------------+-------------+---------+
    +      1      +      7      +  NULL   +
    +-------------+-------------+---------+

tb_Activity    
    +-------------+-------------+---------+
    +     id      + Executor_id +   Type  +
    +-------------+-------------+---------+
    +      1      +      6      +  Follow +
    +-------------+-------------+---------+

so it's User(6) -- follow --> User(7)

the User_id should be 6 in my resolution, the same(and I think duplicated) with Executor_id

1
Is it caused by something related to Polymorphism? - freeflying
I would say: if you want to have both properties to exist: Executor and Followee - then 2 columns are needed. Depends where they are placed in Entities (C#)... the same must be in their table representation. If you want only one User... again, we just have to decide on which level... Or am I missing something? - Radim Köhler
yes, both Executor and Followee are here. But Executor is at Base type Activity. so in the above table, as you can see, User_id is duplicated with Executor_id - freeflying

1 Answers

0
votes

in fact, column User_id is generated by the SchemaExport() in Fluent NHibernate. I removed it and refer KeyColumn("Executor_id") and it works fine.

I think the issue caused by the FluentNHibernate's SchemaExport().