Hi guys,
*This is my model:*
public class Category
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Category Parent { get; set; }
public virtual IList<Category> Children { get; protected set; }
public Category()
{
Children = new List<Category>();
}
}
This is the mapping:
class CategoryMap : ClassMapping<Category>
{
public CategoryMap()
{
Table(typeof(Category).Name);
this.HighLowId(c => c.Id);
Property(c => c.Name);
ManyToOne(c => c.Parent, m => m.Column("ParentId"));
Bag(c => c.Children, m =>
{
m.Key(k => k.Column("ParentId"));
m.Cascade(Cascade.All | Cascade.DeleteOrphans);
m.Inverse(true);
},
m => m.OneToMany());
}
}
*This is the database script:*
create table Category
(
Id int not null,
Name nvarchar(50) not null,
ParentId int null,
constraint PK_Category primary key (Id),
constraint FK_Category_ParentId foreign key (ParentId) references
Category(Id)
)
*This is the test code:*
var parent = new Category { Name = "Category 1" };
var child = new Category
{
Name = "Category 2",
Parent = parent
};
using (var session = Database.OpenSession())
{
using (var tran = session.BeginTransaction())
{
session.Save(parent);
session.Save(child);
tran.Commit();
}
using (var tran = session.BeginTransaction())
{
session.Delete(parent);
tran.Commit(); // Exception:
}
}
The above code will cause this exception:
The DELETE statement conflicted with the SAME TABLE REFERENCE constraint
"FK_Category_ParentId". The conflict occurred in database "Test", table
"dbo.Category", column 'ParentId'. The statement has been terminated."
But if I delete the parent category in a *NEW *session, it will work:
var parent = new Category { Name = "Category 1" };
var child = new Category
{
Name = "Category 2",
Parent = parent
};
using (var session = Database.OpenSession())
{
using (var tran = session.BeginTransaction())
{
session.Save(parent);
session.Save(child);
tran.Commit();
}
}
using (var session = Database.OpenSession())
{
parent = session.Get<Category>(parent.Id);
using (var tran = session.BeginTransaction())
{
session.Delete(parent);
tran.Commit();
}
}
Is this a bug? I'm using NHibernate.3.3.1.4000. Thanks :)
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/nhusers/-/9D6NH4rWi9MJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.