Fluent nhibernate FluentNhibernate和Hasmall

Fluent nhibernate FluentNhibernate和Hasmall,fluent-nhibernate,fluent-nhibernate-mapping,Fluent Nhibernate,Fluent Nhibernate Mapping,我用FluentNhibernate尝试了一个简单的测试应用程序,但它并没有像我预期的那样工作。 以下是我的课程: public class Document : DataEntity { public virtual string Title { get; set; } public virtual string FileName { get; set; } public virtual DateTime LastModificationDate { get; se

我用FluentNhibernate尝试了一个简单的测试应用程序,但它并没有像我预期的那样工作。 以下是我的课程:

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");
    }
公共类文档:DataEntity
{
公共虚拟字符串标题{get;set;}
公共虚拟字符串文件名{get;set;}
公共虚拟日期时间LastModificationDate{get;set;}
公共虚拟用户LastModificationBy{get;set;}
公共虚拟字节[]内容{get;set;}
公共虚拟用户所有者{get;set;}
}
公共类用户:DataEntity
{
公共虚拟字符串FirstName{get;set;}
公共虚拟字符串LastName{get;set;}
公共虚拟字符串登录{get;set;}
公共虚拟字符串密码哈希{get;set;}
公共虚拟字符串电子邮件{get;set;}
公共虚拟IList拥有的文档{get;set;}
公共用户()
{
this.OwnedDocuments=新列表();
}
}
内部类UserMapping:ClassMap
{
公共用户映射()
{
this.Id(x=>x.Id);
this.Map(x=>x.FirstName);
this.Map(x=>x.LastName);
this.HasMany(x=>x.OwnedDocuments).Inverse();
}
}
公共文档映射()
{
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”);
此.References(x=>x.Owner).Column(“Owner”);
本表(“文件”);
}
我尝试用以下代码测试它

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);
        }
使用(var transaction=Session.BeginTransaction())
{
var users=this.kernel.Get();
var document=this.kernel.Get();
var user=新用户{Login=“Blubb”};
用户。添加(用户);
var list=Builder.CreateListOfSize(50).All().With(x=>x.Owner=user.Build().ToList();
var list2=Builder.CreateListOfSize(50).All().Build().ToList();
list.ForEach(x=>user.OwnedDocuments.Add(x));
文件。添加(列表);
文件。添加(列表2);
Commit();
var i=document.All().Count();
i、 应()为(50);
var docs=((IGuidKeyedRepository)users).FindBy(user.Id).OwnedDocuments.Count();
docs.Should()为(50);
}
第一个问题是,当我不调用document.Add(list);,为什么文档计数总是0;?我想当我向用户的文档集合中添加一些文档时,它们会自动添加到文档中吗?
为什么最后一次考试是100分?因为我筛选的文档属于该用户。

看起来您需要在子集合
OwnedDocuments

this.HasMany(x => x.OwnedDocuments).Inverse().Cascade.AllDeleteOrphan();
如果向集合中添加任何子对象,则上述设置将保存所有子对象;如果从集合中删除这些子对象并保存对象,则会删除这些子对象。您可以在nhibernate文档中找到有关这些设置的更多信息: