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