Fluent nhibernate使用Inverse时的一对多子表的外键是空的 [英] Fluent nhibernate One-to-Many when Inverse is used Child table's Foreign key is null
问题描述
public class TableA
{
public virtual long ID {get;组; }
public virtual string Name {get;组; }
公共虚拟IList< TableB> TableB {get;组; }
}
public class TableB
{
public virtual long ID {get;组; }
public virtual string Name {get;组; }
public virtual TableA TableA {get;组; }
}
public class TableAMap:ClassMap< TableA>
{
public TableAMap()
{
Id(x => x.ID);
Map(x => x.Name).Column(Name);
HasMany(x => x.TableB)
.KeyColumn(TableA_ID)
.Inverse()
.Cascase.All()
.Not。 LazyLoad();
}
}
public class TableBMap:ClassMap< TableB>
{
public TableBMap()
{
Id(x => x.ID);
Map(x => x.Name).Column(Name);
References(x => x.TableA).Column(TableA_ID)。Not.LazyLoad();
code
$ b 注当 Inverse()
被从多个移除到一个新的记录插入没有任何问题,外键插入没有任何问题,但是当我更新记录现有的外键记录被替换为空。
请帮我看看类似的问题,但它不帮助我。
链接,这个问题的解决方案。
mapp类应该是
public class TableAMap:类映射<表A>
{
public TableAMap()
{
Id(x => x.ID);
Map(x => x.Name).Column(Name);
HasMany< TableB>(x => x.TableB)
.KeyColumn(TableA_ID)
.Cascase.All()。Inverse();
}
}
public class TableBMap:ClassMap< TableB>
{
public TableBMap()
{
Id(x => x.ID);
Map(x => x.Name).Column(Name);
引用< TableA>(x => x.TableA).Column(TableA_ID)。Not.Nullable();
}
}
I have a parent and child table and entites are created and mapped using one to many relation ship. On one to many side when i use Inverse() then the child table's foreign key value is inserted as null.
public class TableA
{
public virtual long ID { get; set; }
public virtual string Name { get; set; }
public virtual IList<TableB> TableB { get; set; }
}
public class TableB
{
public virtual long ID { get; set; }
public virtual string Name { get; set; }
public virtual TableA TableA { get; set; }
}
public class TableAMap : ClassMap<TableA>
{
public TableAMap()
{
Id(x=>x.ID);
Map(x=>x.Name).Column("Name");
HasMany(x=>x.TableB)
.KeyColumn("TableA_ID")
.Inverse()
.Cascase.All()
.Not.LazyLoad();
}
}
public class TableBMap : ClassMap<TableB>
{
public TableBMap()
{
Id(x=>x.ID);
Map(x=>x.Name).Column("Name");
References(x=>x.TableA).Column("TableA_ID").Not.LazyLoad();
}
}
Note When the Inverse()
is removed from many to one the new records are inserted without any issues and the foreign key is inserted without any issues but when i update a record the foreign key of the existing records are replaced as null.
Please help me i looked in to similar questions but it doesn't help me.
Fluent NHibernate one-to-many relationship setting foreign key to null
Reffer to this link which has the solution for this issue.
The mapp class should be
public class TableAMap : ClassMap<TableA>
{
public TableAMap()
{
Id(x=>x.ID);
Map(x=>x.Name).Column("Name");
HasMany<TableB>(x=>x.TableB)
.KeyColumn("TableA_ID")
.Cascase.All().Inverse();
}
}
public class TableBMap : ClassMap<TableB>
{
public TableBMap()
{
Id(x=>x.ID);
Map(x=>x.Name).Column("Name");
References<TableA>(x=>x.TableA).Column("TableA_ID").Not.Nullable();
}
}
这篇关于Fluent nhibernate使用Inverse时的一对多子表的外键是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!