Hello all,
I have the following two entities, with their mappings:
public class VideoCategory : BaseEntity<VideoCategory>
{
private readonly Iesi.Collections.Generic.ISet<VideoFolder>
folders = new HashedSet<VideoFolder>();
public VideoCategory()
{
}
public VideoCategory(string name)
{
Name = name;
}
public string Name { get; set; }
public IEnumerable<VideoFolder> Folders { get { return
folders; } }
public void AddFolder(VideoFolder videoFolder)
{
Contract.Requires(videoFolder != null);
if (folders.Contains(videoFolder))
return;
folders.Add(videoFolder);
videoFolder.AddCategory(this);
}
public void RemoveFolder(VideoFolder videoFolder)
{
folders.Remove(videoFolder);
videoFolder.RemoveCategory(this);
}
public void ClearFolders()
{
folders.ForEach(f => f.RemoveCategory(this));
folders.Clear();
}
}
public class VideoFolder : BaseEntity<VideoFolder>
{
private readonly Iesi.Collections.Generic.ISet<VideoCategory>
categories = new HashedSet<VideoCategory>();
public VideoFolder()
{
}
public VideoFolder(string path)
{
Path = path;
}
public string Path { get; set; }
public string Name { get; private set; }
public IEnumerable<VideoCategory> Categories { get { return
categories; } }
protected internal void AddCategory(VideoCategory
videoCategory)
{
Contract.Requires(videoCategory != null);
categories.Add(videoCategory);
}
protected internal void RemoveCategory(VideoCategory
videoCategory)
{
categories.Remove(videoCategory);
}
}
public class VideoCategoryMap : ClassMap<VideoCategory>
{
public VideoCategoryMap()
{
Table("VideoCategories");
Id(cat => cat.Id)
.GeneratedBy.Native();
Map(cat => cat.Name)
.Unique()
.Not.Nullable();
HasManyToMany<VideoFolder>(Reveal.Member<VideoCategory>("folders"))
.Access.CamelCaseField()
.AsSet()
.Inverse()
.Cascade.AllDeleteOrphan();
}
}
public class VideoFolderMap : ClassMap<VideoFolder>
{
public VideoFolderMap()
{
Table("VideoFolders");
Id(folder => folder.Id)
.GeneratedBy.Native();
Map(folder => folder.Path)
.Not.Nullable();
HasManyToMany<VideoCategory>(Reveal.Member<VideoFolder>("categories"))
.Access.CamelCaseField()
.AsSet();
}
}
I have these 2 unit-tests:
[Fact]
public void DeletingVideocategory_DeletesVideoFolders()
{
object id;
using (ISession session = SessionFactory.OpenSession())
{
var categ = new VideoCategory("Foo");
var folder = new VideoFolder("D:\\Foo");
categ.AddFolder(folder);
id = session.Save(categ);
session.Flush();
}
using (ISession session = SessionFactory.OpenSession())
{
var category = session.Get<VideoCategory>(id);
category.ClearFolders();
session.Delete(category);
session.Flush();
Assert.Equal(0,
session.QueryOver<VideoFolder>().RowCount());
}
}
[Fact]
public void
DeletingVideocategory_DoesntDeleteVideoFoldersOwned_ByOtherCategories()
{
object id;
object id2;
using (ISession session = SessionFactory.OpenSession())
{
var categ = new VideoCategory("Foo");
var categ2 = new VideoCategory("Bar");
var folder = new VideoFolder("D:\\Foo");
categ.AddFolder(folder);
categ2.AddFolder(folder);
id = session.Save(categ);
id2 = session.Save(categ2);
session.Flush();
}
using (ISession session = SessionFactory.OpenSession())
{
var category = session.Get<VideoCategory>(id);
category.ClearFolders();
session.Delete(category);
session.Flush();
Assert.Equal(1,
session.QueryOver<VideoFolder>().RowCount());
Assert.Equal(1,
session.Get<VideoCategory>(id2).Folders.Count());
}
}
The first one succeeds, but not the second, where the video folder is
deleted, whereas it is still associated with the remaining video
category.
If I change the mapping, and modify Cascade.AllDeleteOrphan in
VideoCategoryMap, the second test succeeds, whereas the first fails,
because the orphaned video folder isn't deleted.
How can I make both tests to succeed?
Thanks in advance
Mike
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
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.