NHibernate not dropping foreign key constraints

▼魔方 西西 提交于 2019-12-11 18:45:30

问题


I'm new to NHibernate, so this is probably my mistake, but when I use:

schema.Create(true, true);

I get:

SchemaExport [(null)]- There is already an object named 'XXX' in the database.
System.Data.SqlClient.SqlException: There is already an object 
named 'XXX' in the database.

I grabbed the SQL code nHibernate was using, ran it directly from MSSMS, and recieved similar errors. Looking into it, the generated code is not properly dropping the foreign key constraints. The drop looks like this:

if exists (select 1 from sysobjects where id = OBJECT_ID(N'dbo[FK22212EAFBFE4C58]')
AND parent_obj = OBJECT_ID('YYY'))
alter table dbo.YYY  drop constraint FK22212EAFBFE4C58

Doing a "select OBJECT_ID(N'dbo[FK22212EAFBFE4C58]')" I get null. If I take out the "dbo" (i.e. "select OBJECT_ID(N'[FK22212EAFBFE4C58]')") then the ID is returned.

So, my question is, why is nHibernate adding the dbo, and why does that prevent the object from being returned (since the table owning the constraint is dbo.XXX)

One of my mapping files:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="CanineApp.Model" assembly="CanineApp.Model" xmlns="urn:nhibernate-mapping-2.2">
  <class name="MedicalLog" table="MedicalLog" schema="dbo">
    <id name="MedicalLogID" type="Int64">
      <generator class="identity" />
    </id>
    <property name="InvoiceAmount" type="Decimal" not-null="true" />
    ...
    <many-to-one name="Canine" class="Canine" column="CanineID" not-null="true" fetch="join" />
    <many-to-one name="TreatmentCategory" class="TreatmentCategory" column="TreatmentCategoryID" not-null="true" access="field.camelcase-underscore" />
  </class>
</hibernate-mapping>

回答1:


Answer: . It's probably a bug. There was an entry for this exact issue for SQL Server 2005. It doesn't appear to have been flagged for SQL 2000, so I'll either create a bug report or fix it.



来源:https://stackoverflow.com/questions/2923395/nhibernate-not-dropping-foreign-key-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!