gpt4 book ai didi

c# - Fluent Nhibernate 保存 child.parentid = parentId on new parent & Child

转载 作者:行者123 更新时间:2023-11-30 20:52:46 27 4
gpt4 key购买 nike

我正在尝试将新的父项目和新的子项目保存到遗留数据库

我的数据很好,当我保存它时,它同时保存了父项和子项。但是 child 的 SillyNameParentId 始终为 0

我无法更改表结构,所以我必须弄清楚如何让它工作。

这里是生成的SQL

NHibernate: INSERT INTO SillyNameParent(Description, Active) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 'Test' [Type: String (1073741823)], @p1 = True [Type: Boolean (0)] NHibernate: INSERT INTO SillyChild (SillyNameCategoryId, sillyNameParentid) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 0 [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)]

最终我希望将@p1 设置为 SillyNameParentId/Parentid

表格:

SillyNameParent
Column PK-IsIdentity: ParentId
Column varchar(255): Description
Column bit: Active

SillyChild //I'm a lookup table
Column int IsIdentity: Id
Column int PK: SillyNameParentId
Column int PK: SillyNameCategoryID

模型:

public class SillyNameParent: Entity
{
public SillyNameParent()
{
Children= new List<SillyChild>();
}
public virtual string AreaOfConcernDesc { get; set; }
public virtual bool Active { get; set; }
public virtual IList<SillyChild> Children{ get; set; }
}

public class SillyChild: Entity
{
public virtual int SillyNameParentId { get; set; }
public virtual int SillyNameCategoryId{ get; set; }
public virtual SillyNameParent Parent { get; set; }

}

map :

public class SillyNameParentMap : IAutoMappingOverride<SillyNameParent>
{
public void Override(AutoMapping<SillyNameParent> mapping)
{
mapping.Table("SillyNameParent");
mapping.Id(x => x.Id).Column("ParentId").GeneratedBy.Identity();
mapping.Map(x => x.Description).Not.Nullable();
mapping.Map(x => x.Active).Nullable();
mapping.HasMany(x => x.children)
.Cascade.All()
.KeyColumn("SillyNameParentId")
.Not
.LazyLoad();
}
}

public class SillyChildMap: IAutoMappingOverride<SillyChild>
{
public void Override(AutoMapping<SillyChild> mapping)
{
mapping.Table("SillyChild");
mapping.Id(x => x.Id).Column("Id").GeneratedBy.Identity();
mapping.Map(x => x.SillyNameParentId).Not.Nullable();
mapping.Map(x => x.SillyNameCategoryId).Not.Nullable();
mapping.HasOne(x => x.Parent).ForeignKey("SillyNameParentId");
}
}

最佳答案

我会说,你快到了。只是映射父/子应该是这样的:

SillyNameParentMap:

public void Override(AutoMapping<SillyNameParent> mapping)
{
...
mapping.HasMany(x => x.Children)
.Cascade.All()
.KeyColumn("SillyNameParentId")
.Inverse() // this is the way how to manage insertions
.Not
.LazyLoad();

傻 child map :

public void Override(AutoMapping<SillyChild> mapping)
{
...
mapping.References(x => x.Parent)
.Column("SillyNameParentId");

这个映射最终会正确插入,就像这样:

var parent = new SillyNameParent();
parent... // set properties

var child = new SillyChild();
child... // set properties

parent.Children.Add(child); // parent knows about child
child.Parent = parent; // always do set the relation both ways

sillyService.SaveSillyParent(snp)

编辑:选择 2 期

异常说明的是双列映射。在 INSERT 或 UPDATE 的情况下这是一个问题。但是使用 NHibernate 我们可以很容易地解决它:

mapping.Map(x => x.SillyNameParentId)
.Not.Nullable()
.Not.Insert()
.Not.Update();
mapping.References(x => x.Parent)
.Column("SillyNameParentId");

所以,现在我们已经映射了两个属性(引用 Parent 及其 int 表示)。只有引用将用于 INSERT、UPDATE。但是两者都可以用于选择、筛选/位置和排序...

关于c# - Fluent Nhibernate 保存 child.parentid = parentId on new parent & Child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575891/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com