使用Fluent NHibernate时出现奇怪的错误。我有一个名为Module的实体对象,如果你愿意,那就是我的“聚合根”。我为Module类和各种相关类创建并测试了我的映射,并尝试创建一个模块,其中填写了所有字段并保存。这工作正常。
我的问题是当我尝试从数据库中检索对象时。 Module对象回来很好,但是它包含的任何集合都是空的,即使我看到数据库表中的对象!我真的可以用一只手,我希望somone可以帮忙!
编辑:在下面添加了BuildDeriveModule的代码。
这是我的测试:
var dei1 = BuildDeriveModule();
var sessionSource = Injector.Resolve<ISessionSource>();
using (var session = sessionSource.CreateSession())
{
using (var transaction = session.BeginTransaction())
{
session.Save(dei1);
transaction.Commit();
}
}
Module item = null;
using (var session = sessionSource.CreateSession())
{
item = session
.CreateCriteria(typeof(Module))
.Add(Restrictions.Eq("ModuleId", dei1.ModuleId))
.UniqueResult<Module>();
Assert.Equal<Module>(dei1, item);
Assert.Equal<int>(4, item.Variables.Count);
}
我的模块类如下所示:(注意:实体来自FluentNHibernate.Data.Entity)
public class Module : Entity, IEntity
{
public Module()
{
Variables = new HashedSet<ModuleVariable>();
}
public virtual string ModuleId
{
get;
set;
}
public virtual ISet<ModuleVariable> Variables
{
get;
set;
}
public virtual Variable StratumVariable
{
get;
set;
}
public virtual Stratum DefaultStratum
{
get;
set;
}
public virtual ISet<Stratum> OptionalStrata
{
get;
set;
}
public virtual DateTime LastUpdated
{
get;
set;
}
#region Override Methods
public override string ToString()
{
return this.ModuleId;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
var other = obj as Module;
if (other == null) return false;
if (other.ModuleId != this.ModuleId) return false;
return true;
}
public override int GetHashCode()
{
return ModuleId.GetHashCode();
}
#endregion
}
我的贴图看起来像这样:
public class ModuleMap : ClassMap<Module>
{
public ModuleMap()
{
Id(x => x.Id);
Version(x => x.LastUpdated);
Map(x => x.ModuleId)
.Unique()
.Not.Nullable();
HasMany<ModuleVariable>(x => x.Variables)
.Inverse()
.Cascade.SaveUpdate();
References<Variable>(x => x.StratumVariable)
.Not.Nullable()
.Cascade.SaveUpdate();
References<Stratum>(x => x.DefaultStratum)
.Not.Nullable()
.Cascade.SaveUpdate();
HasMany<Stratum>(x => x.OptionalStrata)
.Inverse()
.Cascade.SaveUpdate();
}
}
BuildDeriveModule的代码:
private Module BuildDeriveModule() { var pp01 =新关系 { RelationId =“PP01” };
var hh01 = new Relation
{
RelationId = "HH01"
};
var stdei1 = new Variable
{
VariableId = "STDEI1",
Relation = pp01
};
var frameId = new Variable
{
VariableId = "FRAME_ID",
Relation = pp01
};
var persno = new Variable
{
VariableId = "PERSNO",
Relation = pp01
};
var hp = new Driver
{
Name = "HP",
Phase = 1
};
hp.KeyVariables.Add(frameId);
hp.KeyVariables.Add(persno);
var r2p1 = new Variable
{
VariableId = "R2P1",
Relation = pp01
};
var age = new Variable
{
VariableId = "AGE",
Relation = pp01
};
var marst = new Variable
{
VariableId = "MARST",
Relation = pp01
};
var doctp = new Variable
{
VariableId = "DOCTP",
Relation = hh01
};
pp01.AddVariable(stdei1);
pp01.AddVariable(r2p1);
pp01.AddVariable(age);
pp01.AddVariable(marst);
hh01.AddVariable(doctp);
var defaultStratum = new Stratum
{
Number = -1,
Driver = hp
};
var dei1 = new Module
{
ModuleId = "DEI1",
StratumVariable = stdei1,
DefaultStratum = defaultStratum
};
var mv_r2p1 = new ModuleVariable
{
ModuleId = dei1,
VariableId = "R2P1",
UploadId = r2p1,
DownloadId = r2p1,
HasSubunits = true
};
var mv_age = new ModuleVariable
{
ModuleId = dei1,
VariableId = "AGE",
UploadId = age,
DownloadId = age,
HasSubunits = true
};
var mv_marst = new ModuleVariable
{
ModuleId = dei1,
VariableId = "MARST",
UploadId = marst,
DownloadId = marst,
HasSubunits = true
};
var mv_doctp = new ModuleVariable
{
ModuleId = dei1,
VariableId = "DOCTP",
UploadId = doctp,
DownloadId = doctp,
HasSubunits = false
};
dei1.AddVariable(mv_r2p1);
dei1.AddVariable(mv_age);
dei1.AddVariable(mv_marst);
dei1.AddVariable(mv_doctp);
return dei1;
}
答案 0 :(得分:0)
好的,我想我让它工作了。我从最后一行删除了.Inverse()
HasMany<ModuleVariable>(x => x.Variables)
.Inverse()
.Cascade.SaveUpdate();
在ModuleMap中并且单元测试工作正常。我不太确定为什么,所以如果有人能指出这一点,我们将不胜感激。