Fixed
Details
Details
Assignee
Unassigned
UnassignedReporter
Gabriel Schenker
Gabriel SchenkerComponents
Fix versions
Affects versions
Priority
Who's Looking?
Open Who's Looking?
Created April 8, 2008 at 6:04 AM
Updated January 11, 2018 at 10:32 AM
Resolved January 11, 2018 at 10:32 AM
when using a different schema that dbo (SQL Server 2005) the drop schema script
generated by SchemaExport has a bug:
I have a schema 'Playground' in my sql server 2005 database which
contains 3 tables linked by foreign key references. The drop schema
script generates wrong statements for the 'drop constraint' part, e.g.
if exists (select 1 from sys.objects
where object_id = OBJECT_ID(N'Playground[FK742DC1787D6818CB]')
AND parent_object_id = OBJECT_ID('Item'))
alter table Playground.Item drop constraint FK742DC1787D6818CB
Note:
-the point is missing between the schema name (Playground) and the
constraint name in the first part of the where clause
-the schema name is missing in the second part of the where clause
The correct statement would be:
if exists (select 1 from sys.objects
where object_id = OBJECT_ID(N'Playground.[FK742DC1787D6818CB]')
AND parent_object_id = OBJECT_ID('Playground.Item'))
alter table Playground.Item drop constraint FK742DC1787D6818CB
This is my mapping:
================
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Domain"
namespace="Domain"
schema="Playground">
<class name="Category">
<id name="Id">
<generator class="guid"/>
</id>
<set name="Items" cascade="all-delete-orphan">
<key column="CategoryId"/>
<one-to-many class="Item"/>
</set>
</class>
<class name="Item">
<id name="Id">
<generator class="guid"/>
</id>
<set name="ItemMovements" cascade="all-delete-orphan">
<key column="CategoryId"/>
<one-to-many class="ItemMovement"/>
</set>
</class>
<class name="ItemMovement">
<id name="Id">
<generator class="guid"/>
</id>
<property name="WeekOf"/>
</class>
</hibernate-mapping>
and this are the class definitions
=========================
public class IdentityFieldProvider<T> where T : IdentityFieldProvider<T>
{
private Guid _id;
private int? _oldHashCode;
public virtual Guid Id
{
get { return _id; }
set { _id = value; }
}
public override bool Equals(object obj)
{
T other = obj as T;
if (other == null)
return false; // handle the case of comparing two NEW objects
bool otherIsTransient = Equals(other.Id, Guid.Empty);
bool thisIsTransient = Equals(Id, Guid.Empty);
if (otherIsTransient && thisIsTransient)
return ReferenceEquals(other, this);
return other.Id.Equals(Id);
}
public override int GetHashCode()
{
// Once we have a hash code we'll never change it
if (_oldHashCode.HasValue)
return _oldHashCode.Value;
bool thisIsTransient = Equals(Id, Guid.Empty);
// When this instance is transient, we use the base GetHashCode()
// and remember it, so an instance can NEVER change its hash code.
if (thisIsTransient)
{
_oldHashCode = base.GetHashCode();
return _oldHashCode.Value;
}
return Id.GetHashCode();
}
public static bool operator ==(IdentityFieldProvider<T> x, IdentityFieldProvider<T> y)
{
return Equals(x, y);
}
public static bool operator !=(IdentityFieldProvider<T> x, IdentityFieldProvider<T> y)
{
return !(x == y);
}
}
public class NamedEntity<T> : IdentityFieldProvider<T>
where T : NamedEntity<T>
{
public virtual string Name { get; set; }
}
public class Category : NamedEntity<Category>
{
public virtual ISet<Item> Items { get; set; }
public Category()
{
Items = new HashedSet<Item>();
}
}
public class Item : NamedEntity<Item>
{
public virtual ISet<ItemMovement> ItemMovements { get; set; }
public Item()
{
ItemMovements = new HashedSet<ItemMovement>();
}
}
public class ItemMovement : NamedEntity<ItemMovement>
{
public virtual DateTime WeekOf { get; set; }
}