NHibernate SchemaExport无法删除表..有时 [英] NHibernate SchemaExport failing to drop a table .... sometimes

查看:65
本文介绍了NHibernate SchemaExport无法删除表..有时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序的DAL中使用NHibernate,在特定的NHibernate的SchemaExport函数中,在执行单元测试之前删除/重新创建数据库模式.我遇到的问题是,当我运行单元测试并执行SchemaExport时,我的表之一无法每隔第二次删除一次.这将向我表明存在某种外键问题,导致SchemaExport无法删除我的表-但我无法弄清楚.我的架构非常简单-人员表,地址表和人员地址表可支持两者之间的多对多关系.

I'm using NHibernate for the DAL of my application, and in particlular NHibernate's SchemaExport function to drop/recreate my database schema before the execution of unit tests. The issue I'm having is that when I run the unit tests and execute SchemaExport one of my tables fails to drop every second time. This would indicate to me that there is some kind of foreign key issue preventing SchemaExport dropping my table - but I can't figure it out. My schema is very simple - A person table, an Address table and a PersonAddress table to support the many-to-many relationship between the two.

public class Person
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Address> Addresses {get;set;}

    public Person()
    {
        this.Addresses = new List<Address>();
    }

}

public class Address
{
    public virtual int Id { get; set; }
    public virtual string Street1 { get; set; }
    public virtual string Street2 { get; set; }
    public virtual string Postcode { get; set; }

}

和我的NHibernate映射文件...

and my NHibernate mapping files...

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="MyHibernate"
               namespace="MyHibernate"
               >
<class name="Person" table="Person.Person">
    <id name="Id" column="Id">
        <generator class="native" ></generator>
    </id>
    <property name="Name" column="Name" length="50"></property>
    <bag name="Addresses" table="[Person].[PersonAddress]" lazy="false" cascade="all">
        <key column="PersonId" foreign-key="FK_Person_Person_Id"></key>
        <many-to-many class="Address" column="AddressId"></many-to-many>
    </bag>
</class>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="MyHibernate"
               namespace="MyHibernate"
               >
<class name="Address" table="Person.Address">
    <id name="Id" column="Id">
        <generator class="native" ></generator>
    </id>
    <property name="Street1" column="Street1" length="50"></property>
    <property name="Street2" column="Street2" length="50"></property>
    <property name="Postcode" column="Postcode" length="50"></property>
</class>

,当我运行`var cfg = new Configuration();时, cfg.Configure(); cfg.AddAssembly(typeof(Person).Assembly);

and when I run `var cfg = new Configuration(); cfg.Configure(); cfg.AddAssembly(typeof(Person).Assembly);

        new SchemaExport(cfg).Execute(false, true, false, false)

我收到一条SQL异常消息:

I get a SQL exception saying:

MyHibernate.Tests.GenerateSchemaFixture.Can_Generate_Schema: NHibernate.HibernateException:数据库中已经有一个名为"Person"的对象. ----> System.Data.SqlClient.SqlException:数据库中已经有一个名为"Person"的对象.

MyHibernate.Tests.GenerateSchemaFixture.Can_Generate_Schema: NHibernate.HibernateException : There is already an object named 'Person' in the database. ----> System.Data.SqlClient.SqlException : There is already an object named 'Person' in the database.

有什么想法吗?

推荐答案

找到了解决此问题的方法.我只是将SchemaExport的使用分为两个调用.第一个删除现有的架构,第二个重新创建它.

Found a way around this problem. I just broke out the use of SchemaExport into two calls. The first to drop an existing schema, the second to re-create it.

 var cfg = new Configuration();
 cfg.Configure();
 cfg.AddAssembly(typeof(Person).Assembly);

 SchemaExport se = new SchemaExport(cfg);
 //drop database 
 se.Drop(true, true);
 //re-create database
 se.Create(true, true);

在我的测试类的[TestFixtureSetUp]中使用上面的代码效果很好.我现在有一个干净的数据库架构可用于集成测试.

Using the above code in the [TestFixtureSetUp] of my test class works well. I now have a clean database schema to use for integration tests.

这篇关于NHibernate SchemaExport无法删除表..有时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆