Sebastian
Sebastian

Reputation: 972

FluentNhibernate and HasMany

I try a simple Test Application with FluentNhibernate but it doesnt work as I expected. Here are my classes:

public class Document : DataEntity
{
    public virtual string Title { get; set; }

    public virtual string FileName { get; set; }

    public virtual DateTime LastModificationDate { get; set; }

    public virtual User LastModificationBy { get; set; }

    public virtual byte[] Content { get; set; }

    public virtual User Owner { get; set; }
}

public class User : DataEntity
{
    public virtual string FirstName { get; set; }

    public virtual string LastName { get; set; }

    public virtual string Login { get; set; }

    public virtual string PasswordHash { get; set; }

    public virtual string Email { get; set; }

    public virtual IList<Document> OwnedDocuments { get; set; }

    public User()
    {
        this.OwnedDocuments = new List<Document>();
    }
}

internal class UserMapping : ClassMap<User>
{
    public UserMapping()
    {
        this.Id(x => x.Id);
        this.Map(x => x.FirstName);
        this.Map(x => x.LastName);
        this.HasMany(x => x.OwnedDocuments).Inverse();
    }
}

    public DocumentMapping()
    {
        this.Id(x => x.Id);
        this.Map(x => x.Title);
        this.Map(x => x.FileName).Not.Nullable();
        this.Map(x => x.LastModificationDate).Index("IDX_ModificationDate");
        this.Map(x => x.Content);
        this.References(x => x.LastModificationBy).Column("LastModificationBy");
        this.References(x => x.Owner).Column("Owner");
        this.Table("Document");
    }

I try to test it with the following code

using (var transaction = Session.BeginTransaction())
        {
            var users = this.kernel.Get<IRepository<User>>();
            var document = this.kernel.Get<IRepository<Document>>();
            var user = new User { Login = "Blubb" };
            users.Add(user);
            var list = Builder<Document>.CreateListOfSize(50).All().With(x => x.Owner = user).Build().ToList();
            var list2 = Builder<Document>.CreateListOfSize(50).All().Build().ToList();
            list.ForEach(x => user.OwnedDocuments.Add(x));
            document.Add(list);
            document.Add(list2);
            transaction.Commit();
            var i = document.All().Count();
            i.Should().Be(50);
            var docs = ((IGuidKeyedRepository<User>)users).FindBy(user.Id).OwnedDocuments.Count();
            docs.Should().Be(50);
        }

The first problem is, why is the document count always 0 when I dont call document.Add(list);? I thought when I add some documents to the document collection of the user, they will be automaticly added to the documents? And why ist the last test 100? Because I filter on the documents belongs to that user.

Upvotes: 0

Views: 216

Answers (1)

Cole W
Cole W

Reputation: 15313

It looks like you need to set a cascade option on the child collection OwnedDocuments

this.HasMany(x => x.OwnedDocuments).Inverse().Cascade.AllDeleteOrphan();

The setting above will save all the children if you add any to the collection and if you remove them from the collection and save the object it will delete those child objects. You can find out more information about these settings in the nhibernate documentation:

http://www.nhforge.org/doc/nh/en/

Upvotes: 1

Related Questions