gpt4 book ai didi

nhibernate - 流利的 NHibernate 复合 ID 到映射类

转载 作者:行者123 更新时间:2023-12-01 07:30:29 25 4
gpt4 key购买 nike

我想弄清楚如何使用 CompositeId 来映射另一个类。这是一个测试用例:

表:

TestParent:
TestParentId (PK)
FavoriteColor

TestChild:
TestParentId (PK)
ChildName (PK)
Age

C#中的类:
public class TestParent
{
public TestParent()
{
TestChildList = new List<TestChild>();
}

public virtual int TestParentId { get; set; }
public virtual string FavoriteColor { get; set; }
public virtual IList<TestChild> TestChildList { get; set; }
}

public class TestChild
{
public virtual TestParent Parent { get; set; }
public virtual string ChildName { get; set; }
public virtual int Age { get; set; }

public override int GetHashCode()
{
return Parent.GetHashCode() ^ ChildName.GetHashCode();
}

public override bool Equals(object obj)
{
if (obj is TestChild)
{
var toCompare = obj as TestChild;
return this.GetHashCode() != toCompare.GetHashCode();
}
return false;
}
}

Fluent NHibernate 映射:
public class TestParentMap : ClassMap<TestParent>
{
public TestParentMap()
{
Table("TestParent");
Id(x => x.TestParentId).Column("TestParentId").GeneratedBy.Native();
Map(x => x.FavoriteColor);

HasMany(x => x.TestChildList).KeyColumn("TestParentId").Inverse().Cascade.None();
}
}

public class TestChildMap : ClassMap<TestChild>
{
public TestChildMap()
{
Table("TestChild");
CompositeId()
.KeyProperty(x => x.ChildName, "ChildName")
.KeyReference(x => x.Parent, "TestParentId");

Map(x => x.Age);
References(x => x.Parent, "TestParentId"); /** breaks insert **/
}
}

当我尝试添加新记录时,出现此错误:

System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index



我知道这个错误是由于在 CompositeId 和 References 调用中映射了 TestParentId 列。但是,在根据 TestParentId 查询 TestChild 时,删除 References 调用会导致另一个错误。

这是执行查询的代码:
var session = _sessionBuilder.GetSession();
using (var tx = session.BeginTransaction())
{
// create parent
var p = new TestParent() { FavoriteColor = "Red" };
session.Save(p);

// creat child
var c = new TestChild()
{
ChildName = "First child",
Parent = p,
Age = 4
};
session.Save(c); // breaks with References call in TestChildMap

tx.Commit();
}

// breaks without the References call in TestChildMap
var children = _sessionBuilder.GetSession().CreateCriteria<TestChild>()
.CreateAlias("Parent", "p")
.Add(Restrictions.Eq("p.TestParentId", 1))
.List<TestChild>();

关于如何为此场景创建复合键的任何想法?

最佳答案

我找到了一个更好的解决方案,允许查询和插入。关键是更新 TestChild 的映射以不插入记录。新 map 是:

public class TestChildMap : ClassMap<TestChild>
{
public TestChildMap()
{
Table("TestChild");
CompositeId()
.KeyProperty(x => x.ChildName, "ChildName")
.KeyReference(x => x.Parent, "TestParentId");

Map(x => x.Age);
References(x => x.Parent, "TestParentId")
.Not.Insert(); // will avoid "Index was out of range" error on insert
}
}

关于nhibernate - 流利的 NHibernate 复合 ID 到映射类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5378741/

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