Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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# 流利的NHibernate:基本类的ISet_C#_Nhibernate_Fluent Nhibernate - Fatal编程技术网

C# 流利的NHibernate:基本类的ISet

C# 流利的NHibernate:基本类的ISet,c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,在我的项目中,我有一个基类(未映射): 我还有一些继承的类(它们看起来几乎一样,所以这里只有一个类的代码和映射) 公共类用户:BaseEntity { 公共虚拟int用户标识{get;set;} 公共虚拟字符串登录{get;set;} 公共虚拟字符串密码哈希{get;set;} 公共虚拟ISet实体{get;set;} } 公共类UserMap:ClassMap { 公共用户映射() { this.Id(x=>x.UserId); this.Map(x=>x.Login); this.Map(x

在我的项目中,我有一个基类(未映射):

我还有一些继承的类(它们看起来几乎一样,所以这里只有一个类的代码和映射)

公共类用户:BaseEntity
{
公共虚拟int用户标识{get;set;}
公共虚拟字符串登录{get;set;}
公共虚拟字符串密码哈希{get;set;}
公共虚拟ISet实体{get;set;}
}
公共类UserMap:ClassMap
{
公共用户映射()
{
this.Id(x=>x.UserId);
this.Map(x=>x.Login);
this.Map(x=>x.PasswordHash);
this.HasManyToMany(x=>x.Entities);
}
}
接下来,我有一个NHibernateHelper:

public class NHibernateHelper
{
    public static ISession OpenSession()
    {
        ISessionFactory sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(@"someconstring")
            .ShowSql()
        )
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<User>())

        .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))

        .BuildSessionFactory();
        return sessionFactory.OpenSession();
    }
}
公共类NHibernateHelper
{
公共静态会话OpenSession()
{
ISessionFactory sessionFactory=fluntly.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(@“someconstring”)
.ShowSql()
)
.Mappings(m=>m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(cfg=>newschemaupdate(cfg).Execute(false,true))
.BuildSessionFactory();
返回sessionFactory.OpenSession();
}
}
这里有一个问题: 如果我的数据库中需要类似于EnitiyToEntity的表来处理多对多关系,如何从映射中排除BaseEntity类

如果我理解你的问题,解决方案应该是实现TPC(每个具体类的表)

顺便说一下,在映射中,您必须为
hasmanytomy
使用具体类型

例如(我假设您的用户引用了许多组):

在GroupMap类中,您可以像这样引用用户:

    public class Group : BaseEntity
    {
        public virtual int GroupId { get; set; }
        public virtual string PasswordHash { get; set; }
        public virtual ISet<BaseEntity> Members { get; set; }
    }
  HasManyToMany<User>(x => x.Members).Table("UsersGroups");
HasManyToMany(x=>x.Members).Table(“UsersGroups”);

如果引用一个类,则必须映射它。因此,将实体映射为类映射,将所有其他实体映射为子类映射。它们最终将成为union子类,即每个类一个表。不幸的是,你不能用FNH映射hasmanytoany。 您可以将其映射为hasmanytomany并围绕它进行操作:

    var config = new Configuration();

    config.BeforeBindMapping += BeforeBindMapping;
    _config = Fluently
        .Configure(config)
        ...

    private void BeforeBindMapping(object sender, NHCfg.BindMappingEventArgs e)
    {
        var userclass = e.Mapping.RootClasses.FirstOrDefault(rc => rc.name.StartsWith(typeof(User).FullName));
        if (userclass != null)
        {
            HbmSet prop = (HbmSet)paymentclass.Properties.FirstOrDefault(rc => rc.Name == "Entities");
            prop.Item = new HbmManyToAny // == prop.ElementRelationship
            {
                column = new[]
                    {
                        new HbmColumn { name = "entityType", notnull = true, notnullSpecified = true },
                        new HbmColumn { name = "entity_id", notnull = true, notnullSpecified = true }
                    },
                idtype = "Int64",
                metatype = "String",
                metavalue = typeof(Entity).Assembly.GetTypes()
                    .Where(t => !t.IsInterface && !t.IsAbstract && typeof(Entity).IsAssignableFrom(t))
                    .Select(t => new HbmMetaValue { @class = t.AssemblyQualifiedName, value = t.Name })
                    .ToArray()
            };
        }
    }

谢谢你的回答。这不是我特别需要的。数据库必须更像:几个表用于具体类,一个表用于与鉴别器的多对多关系,鉴别器将指向每个成员的具体表。
    public class Group : BaseEntity
    {
        public virtual int GroupId { get; set; }
        public virtual string PasswordHash { get; set; }
        public virtual ISet<BaseEntity> Members { get; set; }
    }
  HasManyToMany<User>(x => x.Members).Table("UsersGroups");
    var config = new Configuration();

    config.BeforeBindMapping += BeforeBindMapping;
    _config = Fluently
        .Configure(config)
        ...

    private void BeforeBindMapping(object sender, NHCfg.BindMappingEventArgs e)
    {
        var userclass = e.Mapping.RootClasses.FirstOrDefault(rc => rc.name.StartsWith(typeof(User).FullName));
        if (userclass != null)
        {
            HbmSet prop = (HbmSet)paymentclass.Properties.FirstOrDefault(rc => rc.Name == "Entities");
            prop.Item = new HbmManyToAny // == prop.ElementRelationship
            {
                column = new[]
                    {
                        new HbmColumn { name = "entityType", notnull = true, notnullSpecified = true },
                        new HbmColumn { name = "entity_id", notnull = true, notnullSpecified = true }
                    },
                idtype = "Int64",
                metatype = "String",
                metavalue = typeof(Entity).Assembly.GetTypes()
                    .Where(t => !t.IsInterface && !t.IsAbstract && typeof(Entity).IsAssignableFrom(t))
                    .Select(t => new HbmMetaValue { @class = t.AssemblyQualifiedName, value = t.Name })
                    .ToArray()
            };
        }
    }