Nhibernate+Nunit 为什么无法生成数据库?

微信公众号 2013-08-06 06:18:00
参考网上的教程学习Nhibernate/Nunit,http://www.cnblogs.com/GoodHelper/archive/2011/02/16/nhibernate_03.html 引用的程序集都是最新的,可是自己的demo就是无法自动生成数据库中的表,求大家指点。
这是我的项目结构:

下面是源码:
NHibernateInit.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NUnit.Framework;

namespace NHibernateTest
{
[TestFixture]
public class NHibernateInit
{
public void InitTest()
{
var cfg = new NHibernate.Cfg.Configuration().Configure(System.AppDomain.CurrentDomain.BaseDirectory + "\\Config\\hibernate.cfg.xml");
using (ISessionFactory sessionFactory = cfg.BuildSessionFactory()) { }
}
}
}

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Server=.;initial catalog=NHibernateDemo;uid=sa;pwd=abao_yatou
</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="show_sql">true</property>
<!--指定用ADO.NET的批量更新的数量-->
<property name="adonet.batch_size">10</property>
<!--指定NHibernate生产的IDbCommands对象的超时时间-->
<property name="command_timeout">60</property>
<!--将Nhibernate查询中的符号映射到SQL查询中的符号-->
<property name="query.substitutions">true 1,false 0,yes 'Y',no 'N'</property>

<mapping assembly="Domain"/>
</session-factory>
</hibernate-configuration>

Product.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain">
<class name="Product" table="T_Product" lazy="true">
<id name="ID" column="ID">
<generator class="assigned"></generator>
</id>
<property name="Code" column="Code" type="String" length="50"></property>
<property name="Name" type="String" column="Name" length="50"></property>
<property name="QuantityPerUnit" type="String" column="QuantityPerUnit" length="50"></property>
<property name="Unit" type="String" column="Unit" length="50"></property>
<property name="SellPrice" type="decimal" column="SellPrice" precision="14" scale="2"></property>
<property name="BuyPrice" type="decimal" column="BuyPrice" precision="14" scale="2"></property>
<property name="Remark" type="String" column="Remark" length="200"></property>
</class>
</hibernate-mapping>

Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain
{
/// <summary>
/// 商品
/// </summary>
public class Product
{
/// <summary>
/// ID
/// </summary>
public virtual Guid ID { get; set; }

/// <summary>
/// 编号
/// </summary>
public virtual string Code { get; set; }

/// <summary>
/// 名称
/// </summary>
public virtual string Name { get; set; }

/// <summary>
/// 规格
/// </summary>
public virtual string QuantityPerUnit { get; set; }

/// <summary>
/// 单位
/// </summary>
public virtual string Unit { get; set; }

/// <summary>
/// 售价
/// </summary>
public virtual decimal SellPrice { get; set; }

/// <summary>
/// 进价
/// </summary>
public virtual decimal BuyPrice { get; set; }

/// <summary>
/// 备注
/// </summary>
public virtual string Remark { get; set; }
}
}
...全文
181 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
itaaaguo 2014-07-17
  • 打赏
  • 举报
回复
我的数据库是oracle的 为什么也建造不出来表呢
微信公众号 2013-08-07
  • 打赏
  • 举报
回复
引用 6 楼 jshi123 的回复:
你用cfg.ClassMappings.Count看下映射是否成功,应该是1 你在调试状态下看输出窗口,执行到new SchemaExport(cfg).Execute(true, true, false)这句时,屏幕上是不是有显示:

    if exists (select * from dbo.sysobjects where id = object_id(N'T_Product') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table T_Product

    create table T_Product (
        ID UNIQUEIDENTIFIER not null,
       Code NVARCHAR(50) null,
       Name NVARCHAR(50) null,
       QuantityPerUnit NVARCHAR(50) null,
       Unit NVARCHAR(50) null,
       SellPrice DECIMAL(14, 2) null,
       BuyPrice DECIMAL(14, 2) null,
       Remark NVARCHAR(200) null,
       primary key (ID)
    )
找到原因了,原来NUnit的最新版本是2.6.2,对.NET3.5完全支持,但是我用vs12建的项目是.NET4.5,NUnit 并不是支持的很好,所有就出现了建表语句无法执行,但是其他单元测试方法可以正常执行的现象。解决方法是:把项目的目标框架改为3.5 就一切OK了。
微信公众号 2013-08-07
  • 打赏
  • 举报
回复
引用 6 楼 jshi123 的回复:
你用cfg.ClassMappings.Count看下映射是否成功,应该是1 你在调试状态下看输出窗口,执行到new SchemaExport(cfg).Execute(true, true, false)这句时,屏幕上是不是有显示:

    if exists (select * from dbo.sysobjects where id = object_id(N'T_Product') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table T_Product

    create table T_Product (
        ID UNIQUEIDENTIFIER not null,
       Code NVARCHAR(50) null,
       Name NVARCHAR(50) null,
       QuantityPerUnit NVARCHAR(50) null,
       Unit NVARCHAR(50) null,
       SellPrice DECIMAL(14, 2) null,
       BuyPrice DECIMAL(14, 2) null,
       Remark NVARCHAR(200) null,
       primary key (ID)
    )
映射成功,是1。可输出窗口不显示sql语句。我新添加了一个winForm项目,在winform里调用单元测试 NHibernateInit下的InitTest方法,就可以正常输出建表语句,数据库里的T_Product表也自动生成了。可为什么单元测试不可以?又或者我的方法不对?
jshi123 2013-08-06
  • 打赏
  • 举报
回复
你用cfg.ClassMappings.Count看下映射是否成功,应该是1 你在调试状态下看输出窗口,执行到new SchemaExport(cfg).Execute(true, true, false)这句时,屏幕上是不是有显示:

    if exists (select * from dbo.sysobjects where id = object_id(N'T_Product') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table T_Product

    create table T_Product (
        ID UNIQUEIDENTIFIER not null,
       Code NVARCHAR(50) null,
       Name NVARCHAR(50) null,
       QuantityPerUnit NVARCHAR(50) null,
       Unit NVARCHAR(50) null,
       SellPrice DECIMAL(14, 2) null,
       BuyPrice DECIMAL(14, 2) null,
       Remark NVARCHAR(200) null,
       primary key (ID)
    )
微信公众号 2013-08-06
  • 打赏
  • 举报
回复
引用 4 楼 jshi123 的回复:
没有看到你有建表的语句啊,加上下面这句试试: var cfg = new NHibernate.Cfg.Configuration().Configure(System.AppDomain.CurrentDomain.BaseDirectory + "\\Config\\hibernate.cfg.xml"); new SchemaExport(cfg).Execute(true, true, false);
这应该是根据映射文件自动生成的,不用写建表语句啊。加上这句,还是不行
jshi123 2013-08-06
  • 打赏
  • 举报
回复
没有看到你有建表的语句啊,加上下面这句试试: var cfg = new NHibernate.Cfg.Configuration().Configure(System.AppDomain.CurrentDomain.BaseDirectory + "\\Config\\hibernate.cfg.xml"); new SchemaExport(cfg).Execute(true, true, false);
微信公众号 2013-08-06
  • 打赏
  • 举报
回复
怎么没人搭理呀……
微信公众号 2013-08-06
  • 打赏
  • 举报
回复
在线等,大家都给点意见啊
微信公众号 2013-08-06
  • 打赏
  • 举报
回复
xml文件的属性也设置了,数据库也提前建立了。问题就是无法自动生成Product表?

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧