NHibernate not dropping foreign key constraints.

Posted by Kendrick on Stack Overflow See other posts from Stack Overflow or by Kendrick
Published on 2010-05-27T17:31:52Z Indexed on 2010/05/31 15:23 UTC
Read the original article Hit count: 167

Filed under:
|
|

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>

© Stack Overflow or respective owner

Related posts about sql

Related posts about nhibernate