1

我的系统中有一个类的用户映射,这些用户可以彼此成为朋友,所以每个用户都有一个他们是朋友的用户列表,但是当我创建它时,没有朋友表,也没有列在FriendList创建的用户表中。

public class User
{
    public User()
    {

        Games = new List<Game>();
        Stats = new UserStats { Rating = 1500 };
    }

    public User(string username, string password)
    {
        Username = username;
        Password = password;
        Games = new List<Game>();
        FriendList = new List<User>();
        Stats = new UserStats { Rating = 1500 };
    }

    public virtual int Id { get; set; }
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    public virtual IList<Game> Games { get; set; }
    public virtual UserStats Stats { get; set; }
    public virtual IList<User> FriendList { get; set; } 
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id)
            .Column("UserId")
            .GeneratedBy
            .HiLo("100");
        Map(x => x.Username)
            .Not.Nullable();
        Map(x => x.Password)
            .Not.Nullable();
        HasMany(x => x.Games)
            .Cascade.All();
        References(x => x.Stats)
            .Not.Nullable()
            .Cascade.All();
        HasMany(x => x.FriendList)
            .Cascade.All();
    }
}

我应该改为使用这些设置创建一个friendtable吗?

public class FriendConnection
{
    public int Id;
    public int Friend1ID;
    public int Friend2ID;
}

或者我可以让这个其他设置工作吗?

这是我使用的会话工厂

public class SessionFactory
{
    private static readonly string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;

    private static ISessionFactory _session;
    private static readonly object SyncRoot = new Object();

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(MySQLConfiguration
            .Standard
            .ConnectionString(ConnString))
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<UserMap>())
                .ExposeConfiguration(UpdateSchema)
                .BuildSessionFactory();
    }
    //
    private static void UpdateSchema(Configuration cfg)
    {
        new SchemaUpdate(cfg).Execute(false, true);
    }

    public static ISession Session
    {
        get
        {
            if (_session == null)
            {
                lock (SyncRoot)
                {
                    if (_session == null)
                        _session = CreateSessionFactory();
                }
            }
            return _session.OpenSession();
        }
    }

    private ISessionFactory CreateTables()
    { 
        return Fluently.Configure()
            .Database(MySQLConfiguration
            .Standard
            .ConnectionString(ConnString))
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<UserMap>())
                .BuildSessionFactory();
    }

    private static void ExportSchema(Configuration cfg)
    {
        var schemaexport = new SchemaExport(cfg);
        schemaexport.Drop(false, true);
        schemaexport.Create(false, true);

    }

    public ISession OpenCreateTablesSession()
    {

        _session = _session ?? (_session = CreateTables());

        return _session.OpenSession();
    }
}
4

2 回答 2

1

您的无参数构造函数不会实例化您的好友列表

public User()
{
    Games = new List<Game>();
    Stats = new UserStats { Rating = 1500 };
    FriendList = new List<User>(); // <<<--- missing
 }
于 2013-01-12T13:37:29.407 回答
1

如果任何用户可以成为任何用户的朋友,您将需要 ManyToMany 映射:

HasManyToMany(x => x.FriendList)
    .Table("FriendConnection")
    .ParentKeyColumn("Friend1ID")
    .ChildKeyColumn("Friend2ID");

不需要 C# 实体作为中间人

但问题是,这样的 Friend 列表当前实例为Parent owner。所以我(作为用户)拥有关系(作为父母)。换句话说,我也可能是其他参考的孩子。这就是为什么用户还应该拥有 Friends 的集合,当前实例在其中发挥child作用

HasManyToMany(x => x.FriendList2)
    .Table("FriendConnection")
    .ParentKeyColumn("Friend2ID")
    .ChildKeyColumn("Friend1ID");  // reversed mapping

用户应该像这样扩展:

public virtual IList<User> FriendList { get; set; }
public virtual IList<User> FriendList2 { get; set; }
于 2013-01-12T13:46:27.613 回答