Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有用户的体量表_C#_Nhibernate_Fluent Nhibernate - Fatal编程技术网

C# 具有用户的体量表

C# 具有用户的体量表,c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,我在我的系统中有一个类的用户映射,这些用户可以彼此成为朋友,因此每个用户都有一个他们是朋友的用户列表,但是当我创建它时,在创建的用户表中没有朋友表,也没有包含FriendList的列 public class User { public User() { Games = new List<Game>(); Stats = new UserStats { Rating = 1500 }; } public User(s

我在我的系统中有一个类的用户映射,这些用户可以彼此成为朋友,因此每个用户都有一个他们是朋友的用户列表,但是当我创建它时,在创建的用户表中没有朋友表,也没有包含
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 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();
    }
}
公共类SessionFactory
{
私有静态只读字符串ConnString=System.Configuration.ConfigurationManager.ConnectionString[“MySQLConnectionString”].ConnectionString;
私有静态ISessionFactory会话;
私有静态只读对象SyncRoot=新对象();
私有静态ISessionFactory CreateSessionFactory()
{
流畅地返回。Configure()
.数据库(MySQLConfiguration
标准
.ConnectionString(连接字符串))
.Mappings(m=>m.FluentMappings
.AddFromAssemblyOf())
.ExposeConfiguration(UpdateSchema)
.BuildSessionFactory();
}
//
私有静态void UpdateSchema(配置cfg)
{
newschemaupdate(cfg).Execute(false,true);
}
公众休会期
{
得到
{
如果(_session==null)
{
锁定(同步根)
{
如果(_session==null)
_会话=CreateSessionFactory();
}
}
返回_session.OpenSession();
}
}
私有ISessionFactory CreateTables()
{ 
流畅地返回。Configure()
.数据库(MySQLConfiguration
标准
.ConnectionString(连接字符串))
.Mappings(m=>m.FluentMappings
.AddFromAssemblyOf())
.BuildSessionFactory();
}
私有静态void ExportSchema(配置cfg)
{
var schemaexport=新schemaexport(cfg);
schemaexport.Drop(false,true);
schemaexport.Create(false,true);
}
公共ISession OpenCreateTableSession()
{
_会话=_会话??(_会话=CreateTables());
返回_session.OpenSession();
}
}

您的无参数构造函数不会实例化您的friendslist

public User()
{
    Games = new List<Game>();
    Stats = new UserStats { Rating = 1500 };
    FriendList = new List<User>(); // <<<--- missing
 }
公共用户()
{
游戏=新列表();
Stats=newuserstats{Rating=1500};

FriendList=new List();//如果任何用户都可以成为任何用户的朋友,您将需要许多映射:

HasManyToMany(x => x.FriendList)
    .Table("FriendConnection")
    .ParentKeyColumn("Friend1ID")
    .ChildKeyColumn("Friend2ID");
不需要中间人这样的C#实体

但问题是,这样一个好友列表的当前实例作为父所有者。因此,我(作为用户)拥有关系(作为父)。换句话说,我也可能是其他引用的子对象。这就是为什么用户还应该拥有好友集合,其中当前实例扮演
子对象
角色

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; }
public虚拟IList好友列表{get;set;}
公共虚拟IList FriendList2{get;set;}

HasManyToMany(x=>x.Friends2)应该是HasManyToMany(x=>x.friendslist2),因此它是对公共虚拟IList FriendList2{get;set;}的引用我把
朋友
重新命名为
朋友列表
,正如你所说的!;)很酷,现在它成了表!但我怀疑我现在应该如何添加朋友关系?Atm我有,但由于每个用户现在有2个列表,我现在如何添加朋友?好问题;)难回答。在标准关系中,多对多很容易,因为nt只扮演父角色,子角色是子角色。但在这里,您应该引入更多的逻辑。如果用户正在添加新朋友…它应该充当父角色并扩展
好友列表
。当用户被其他人引用时,他应该充当子角色(并将自动显示在
好友列表2
)。更多的业务逻辑,以避免重复并呈现两个集合中的关系…这些将是下一步。毫无疑问,如果用户id 1将用户id 2添加为好友,那么不同列表中会有哪些数据?当我查看列表以查看谁是某个用户的好友时,我会查看哪个列表?
public virtual IList<User> FriendList { get; set; }
public virtual IList<User> FriendList2 { get; set; }