运行时的Nhibernate映射

运行时的Nhibernate映射,nhibernate,fluent-nhibernate,nhibernate-mapping,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,我正在开发一个nhibernate正在使用的网站。这对于静态映射来说很好。但问题是我在现有数据库上应用这个应用程序。那么,是否有任何方式可以在运行时进行类的映射呢。我的意思是用户为映射提供表和列名。谢谢我认为NHibernate不可能做到这一点,但您可以使用一种变通方法。 对于NHibernate映射,可以使用视图代替表 在运行时,您可以创建该视图,或者使用所需的特定用户映射对其进行更新 例如,您可以在NHibernate中定义一个到名为ViewMapped的视图的映射,该视图有两列Name和M

我正在开发一个nhibernate正在使用的网站。这对于静态映射来说很好。但问题是我在现有数据库上应用这个应用程序。那么,是否有任何方式可以在运行时进行类的映射呢。我的意思是用户为映射提供表和列名。谢谢

我认为NHibernate不可能做到这一点,但您可以使用一种变通方法。 对于NHibernate映射,可以使用视图代替表

在运行时,您可以创建该视图,或者使用所需的特定用户映射对其进行更新

例如,您可以在NHibernate中定义一个到名为
ViewMapped
的视图的映射,该视图有两列
Name
Mail
。 另一方面,用户有一个包含三列的表
Name
SecondName
EMail
。 您可以使用以下选项在运行时创建视图:

(SELECT Name + ' ' + SecondName as Name, EMail as Mail FROM tableName) AS ViewMapped

我希望这对您有所帮助,或者至少能引导您找到解决方案。

我认为NHibernate不可能做到这一点,但您可以使用一种变通方法。 对于NHibernate映射,可以使用视图代替表

在运行时,您可以创建该视图,或者使用所需的特定用户映射对其进行更新

例如,您可以在NHibernate中定义一个到名为
ViewMapped
的视图的映射,该视图有两列
Name
Mail
。 另一方面,用户有一个包含三列的表
Name
SecondName
EMail
。 您可以使用以下选项在运行时创建视图:

(SELECT Name + ' ' + SecondName as Name, EMail as Mail FROM tableName) AS ViewMapped

我希望这能帮助您,或者至少能为您找到一个解决方案。

从您的问题中,我解释您说POCO类存在,但您在构建时不知道表或列名

如果你已经上过这门课:

public class MyGenericClass 
{
    public virtual long Id { get; set; }
    public virtual string Title { get; set; }
}
您可以在运行时将其绑定到表和列:

 string tableName; // Set somewhere else by user input
 string idColumnName; // Set somewhere else by user input
 string titleColumnName; // Set somewhere else by user input

 var configuration = new NHibernate.Cfg.Configuration();
 configuration.Configure();

 var mapper = new NHibernate.Mapping.ByCode.ModelMapper();

 mapper.Class<MyGenericClass>(
           classMapper =>
           {
                classMapper.Table(tableName);
                classMapper.Id(
                      myGenericClass => myGenericClass.Id,
                      idMapper =>
                      {
                          idMapper.Column(idColumnName);
                          idMapper.Generator(Generators.Identity);
                      }
                 );
                 classMapper.Property(c => c.Title,
                                      propertyMapper =>
                                      {
                                          propertyMapper.Column(titleColumnName);
                                      }
                                     );
           }
 );

 ISessionFactory sessionFactory = configuration.BuildSessionFactory();
 ISession session = sessionFactory.OpenSession(); 

 ////////////////////////////////////////////////////////////////////
 // Now we can run an SQL query over this newly specified table
 //
 List<MyGenericClass> items = session.QueryOver<MyGenericClass>().List();
string tableName;//通过用户输入设置其他位置
字符串idColumnName;//通过用户输入设置其他位置
字符串titleColumnName;//通过用户输入设置其他位置
var configuration=new NHibernate.Cfg.configuration();
Configure.Configure();
var mapper=new NHibernate.Mapping.ByCode.ModelMapper();
映射器类(
类映射器=>
{
表(tableName);
classMapper.Id(
myGenericClass=>myGenericClass.Id,
idMapper=>
{
idMapper.Column(idColumnName);
idMapper.Generator(Generators.Identity);
}
);
属性(c=>c.Title,
propertyMapper=>
{
propertyMapper.Column(titleColumnName);
}
);
}
);
ISessionFactory sessionFactory=configuration.BuildSessionFactory();
ISession session=sessionFactory.OpenSession();
////////////////////////////////////////////////////////////////////
//现在,我们可以对这个新指定的表运行SQL查询
//
列表项=session.QueryOver().List();

根据您的问题,我理解您的意思是POCO类存在,但您在构建时不知道表名或列名

如果你已经上过这门课:

public class MyGenericClass 
{
    public virtual long Id { get; set; }
    public virtual string Title { get; set; }
}
您可以在运行时将其绑定到表和列:

 string tableName; // Set somewhere else by user input
 string idColumnName; // Set somewhere else by user input
 string titleColumnName; // Set somewhere else by user input

 var configuration = new NHibernate.Cfg.Configuration();
 configuration.Configure();

 var mapper = new NHibernate.Mapping.ByCode.ModelMapper();

 mapper.Class<MyGenericClass>(
           classMapper =>
           {
                classMapper.Table(tableName);
                classMapper.Id(
                      myGenericClass => myGenericClass.Id,
                      idMapper =>
                      {
                          idMapper.Column(idColumnName);
                          idMapper.Generator(Generators.Identity);
                      }
                 );
                 classMapper.Property(c => c.Title,
                                      propertyMapper =>
                                      {
                                          propertyMapper.Column(titleColumnName);
                                      }
                                     );
           }
 );

 ISessionFactory sessionFactory = configuration.BuildSessionFactory();
 ISession session = sessionFactory.OpenSession(); 

 ////////////////////////////////////////////////////////////////////
 // Now we can run an SQL query over this newly specified table
 //
 List<MyGenericClass> items = session.QueryOver<MyGenericClass>().List();
string tableName;//通过用户输入设置其他位置
字符串idColumnName;//通过用户输入设置其他位置
字符串titleColumnName;//通过用户输入设置其他位置
var configuration=new NHibernate.Cfg.configuration();
Configure.Configure();
var mapper=new NHibernate.Mapping.ByCode.ModelMapper();
映射器类(
类映射器=>
{
表(tableName);
classMapper.Id(
myGenericClass=>myGenericClass.Id,
idMapper=>
{
idMapper.Column(idColumnName);
idMapper.Generator(Generators.Identity);
}
);
属性(c=>c.Title,
propertyMapper=>
{
propertyMapper.Column(titleColumnName);
}
);
}
);
ISessionFactory sessionFactory=configuration.BuildSessionFactory();
ISession session=sessionFactory.OpenSession();
////////////////////////////////////////////////////////////////////
//现在,我们可以对这个新指定的表运行SQL查询
//
列表项=session.QueryOver().List();

我认为你不能用NHibernate来做这件事。我假设您希望使用NHibernate映射任何数据库的任何字段,前提是用户向您展示如何进行映射以填充编译对象。但我认为你做不到。但我认为使用视图可能有一个变通方法。我会把它贴在一个答案上,你需要一个支持动态的ORM(除非POCO类存在——正如AndrewShepherd在下面提到的那样)。这里有一个很好的例子:我认为你不能用NHibernate来做这个。我假设您希望使用NHibernate映射任何数据库的任何字段,前提是用户向您展示如何进行映射以填充编译对象。但我认为你做不到。但我认为使用视图可能有一个变通方法。我会把它贴在一个答案上,你需要一个支持动态的ORM(除非POCO类存在——正如AndrewShepherd在下面提到的那样)。下面是一个很好的例子: