C# 从 NHibernate 配置文件生成数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/432118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate Database from NHibernate config files
提问by NotDan
Is it possible to generate the database tables and c# classes from NHibernate config files? Afterwards, is it possible to change the config files and update the tables and config files non-destructively?
是否可以从 NHibernate 配置文件生成数据库表和 c# 类?之后,是否可以非破坏性地更改配置文件并更新表和配置文件?
Do you recommend any tools to do this? (preferably free...)
你推荐任何工具来做到这一点吗?(最好是免费的...)
采纳答案by Nikolaos Georgiou
As mentioned by Joachim, it's the "hbm2ddl.auto" setting that you're looking for.
正如 Joachim 所提到的,这是您正在寻找的“hbm2ddl.auto”设置。
You can set it by code like this:
您可以通过这样的代码设置它:
var cfg = new NHibernate.Cfg.Configuration();
cfg.SetProperty("hbm2ddl.auto", "create");
cfg.Configure();
And you can also set it in your App.config file:
你也可以在你的 App.config 文件中设置它:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="hbm2ddl.auto">create</property>
回答by Shiva
Yes it is possible to generate the database tables and C# classes from the NHibernate config files. Let me explain with this example here .
是的,可以从 NHibernate 配置文件生成数据库表和 C# 类。让我在这里用这个例子来解释。
1 : Address.hbm.xml
1:地址.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.0"
default-cascade="none">
<class
name="Your.Domain.AddressImpl, Your.Domain"
table="[ADDRESS]"
dynamic-insert="true"
dynamic-update="true"
lazy="true">
<id name="Id" type="long" unsaved-value="0">
<column name="ID" sql-type="NUMERIC(19,0)"/>
<generator class="native"> </generator>
</id>
<property name="Address1" type="string">
<column name="ADDRESS1" not-null="true" unique="false" sql-type="VARCHAR(100)"/>
</property>
<property name="Address2" type="string">
<column name="ADDRESS2" not-null="true" unique="false" sql-type="VARCHAR(100)"/>
</property>
<property name="City" type="string">
<column name="CITY" not-null="true" unique="false" sql-type="VARCHAR(100)"/>
</property>
<property name="State" type="string">
<column name="STATE" not-null="true" unique="false" sql-type="VARCHAR(100)"/>
</property>
<property name="Zipcode" type="string">
<column name="ZIPCODE" not-null="true" unique="false" sql-type="VARCHAR(100)"/>
</property>
</class>
</hibernate-mapping>
Step 2 : Just by the looking at the configuration file , you Address object siply looks like the following
第 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
/// <summary>
///
/// </summary>
public virtual string Address1
{
get { return _address1; }
set { this._address1 = value; }
}
/// <summary>
///
/// </summary>
public virtual string Address2
{
get { return _address2; }
set { this._address2 = value; }
}
/// <summary>
///
/// </summary>
public virtual string City
{
get { return _city; }
set { this._city = value; }
}
/// <summary>
///
/// </summary>
public virtual long Id
{
get { return _id; }
set { this._id = value; }
}
/// <summary>
///
/// </summary>
public virtual string State
{
get { return _state; }
set { this._state = value; }
}
/// <summary>
///
/// </summary>
public virtual string Zipcode
{
get { return _zipcode; }
set { this._zipcode = value; }
}
#endregion
}
}
Step 3 : And again if you look at the "Column" name property and its data type that is actually refers to the Actual backend database table called "Address".
第 3 步:再次查看“列”名称属性及其数据类型,该数据类型实际上是指名为“地址”的实际后端数据库表。
There are also huge amount of tools that help you to generate all these artifacts for you based on different input such as UML, Or Actual Database schema etc.
还有大量工具可以帮助您根据不同的输入(例如 UML、或实际数据库模式等)为您生成所有这些工件。
回答by Joachim Kerschbaumer
check out the "hbm2ddl.auto" setting (in configuration or NHibernate.Cfg.Configuration ). with "create" you can recreate the whole database schema from your mappings and "update" setting should just update your schema.
检查“hbm2ddl.auto”设置(在配置或 NHibernate.Cfg.Configuration 中)。使用“创建”,您可以从映射重新创建整个数据库架构,而“更新”设置应该只更新您的架构。