How do I setup nhibernate to handle this?
I currently have this code.
tableB = new TableB
{
TableA = tableA,
};
List<TableC> tableC = locations.Select(location => new TableC
{
TableB = tableB
}).ToList();
tableB.TableC = tableC;
tableA.TableB.Add(tableB);
nhibernate.Create(a);
nhibernate.Commit();
The above code works but I find it kinda weird that I have do it like this.
I would like to do something like
tableB = new TableB
{
TableA = tableA,
TableC = MakeAllTableCs()
};
tableA.TableB.Add(tableB);
nhibernate.Create(a);
nhibernate.Commit();
The collection of tableC's is being made in memory and and when I try to do a create I get
not-null property references a null or transient value
It seems that it want's a reference to TableB for each one in the collection of TableC. It seems kinda odd to do this seeing that I am sticking in the TableB object. I would have hoped that it would have figured that out and used it as a reference.
Is there anyway that I can do it so I don't need to have a reference of TableB in each of my TableC objects?
Edit Mapping
public class TableAMap : ClassMap<TableA>
{
public TableAMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
HasMany(x 开发者_运维知识库=> x.).Cascade.All().Inverse();
}
}
public class TableBMap : ClassMap<TableB>
{
public TableBMap ()
{
Id(x => x.Id).GeneratedBy.GuidComb();
References(x => x.TableA).Not.Nullable();
HasMany(x => x.TableC).Cascade.All().Inverse();
}
}
public class TableCMap : ClassMap<TableC>
{
public TableCMap ()
{
Id(x => x.Id).GeneratedBy.GuidComb();
References(x => x.TableB).Not.Nullable();
}
}
Hard to say without your mappings and real NHibernate code (where you call session.Save()
) but most likely you are not saving the new TableB
objects and also don't have any cascade setting on TableA.b
(you need one of those).
精彩评论