用户批量表

时间:2013-01-12 13:21:53

标签: c# nhibernate fluent-nhibernate

我的系统中有一个类的用户映射,这些用户可以是彼此的朋友,所以每个用户都有一个他们是朋友的用户列表,但是当我创建它时,没有用于朋友的表并且在用户表中没有创建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();
    }
}

我应该改为使用这些设置吗?

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

2 个答案:

答案 0 :(得分:1)

您的无参数构造函数未实例化您的朋友列表

答案 1 :(得分:1)

如果任何用户可能是任何用户的朋友,您将需要ManyToMany映射:

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

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

但问题是,这样的朋友列表将当前实例作为父所有者。所以我(作为用户)拥有该关系(作为)。换句话说,我也可以是其他参考的 child 。这就是为什么用户还应该拥有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; }