从NHibernate配置文件生成数据库

是否可以从NHibernate配置文件生成数据库表和c#类? 之后,是否可以非破坏性地更改配置文件并更新表和配置文件?

你推荐使用任何工具吗? (最好免费…)

正如Joachim所说,这是你正在寻找的“hbm2ddl.auto”设置。

您可以通过以下代码进行设置:

var cfg = new NHibernate.Cfg.Configuration(); cfg.SetProperty("hbm2ddl.auto", "create"); cfg.Configure(); 

您还可以在App.config文件中进行设置:

    create 

检查“hbm2ddl.auto”设置(在配置或NHibernate.Cfg.Configuration中)。 使用“创建”,您可以从映射重新创建整个数据库模式,“更新”设置应该只更新您的模式。

是的,可以从NHibernate配置文件生成数据库表和C#类。 让我在这里用这个例子来解释。

1:Address.hbm.xml

                          

第2步:只需查看配置文件,您就可以使用Address对象siply,如下所示

 using System; namespace Your.Domain { public partial class Address { #region Attributes and Associations private string _address1; private string _address2; private string _city; private long _id; private string _state; private string _zipcode; #endregion #region Properties ///  /// ///  public virtual string Address1 { get { return _address1; } set { this._address1 = value; } } ///  /// ///  public virtual string Address2 { get { return _address2; } set { this._address2 = value; } } ///  /// ///  public virtual string City { get { return _city; } set { this._city = value; } } ///  /// ///  public virtual long Id { get { return _id; } set { this._id = value; } } ///  /// ///  public virtual string State { get { return _state; } set { this._state = value; } } ///  /// ///  public virtual string Zipcode { get { return _zipcode; } set { this._zipcode = value; } } #endregion } } 

第3步:再次,如果您查看“列”名称属性及其数据类型实际上是指名为“地址”的实际后端数据库表。

还有大量的工具可以帮助您根据不同的输入(如UML或实际数据库架构等)为您生成所有这些工件。