c# - NHibernate 父表和子表在其组合中具有不同数量的键
问题描述
我在控制台应用程序中使用 NHibernate 映射有点挣扎。
这是摘要:
这是一对多的关系。PARENT_TABLE(一个)到 CHILD_TABLE(多个)
PARENT_TABLE 有一个复合键,由两个键组成:供应商和发票
Id(x => x.Invoice).Column("INVOICE");
References(x => x.Distribution).Column("INVOICE");
- CHILD_TABLE 有一个由三个组成的复合键:Vendor、Invoice 和 Num
Id(x => x.Invoice).Column("INVOICE");
HasOne(x => x.Invoice).ForeignKey("INVOICE").Cascade.Refresh();
当我运行一些具有以下示例的数据时:
供应商发票编号
10 | 44 | 1
11 | 44 | 1
11 | 44 | 2
由于发票中的重复 ID,我最终得到了一个错误(完全有意义)
然后,我尝试使用复合 ID,其中 3 个键用于分发,2 个键用于发票。正如我相信你们中的许多人已经知道的那样,这也以错误告终,因为有两条记录分别使用 11 和 44 作为供应商和发票。
我的问题:有没有办法使用复合 ID(INVOICE 和 VENDOR)声明关系,并且仍然让子集合尊重/强制 3 键复合(INVOICE、VENDOR 和 NUM)的唯一性?
我已经尝试了几种组合键的排列,但无法弄清楚。也许它不是为此而设计的。任何帮助将非常感激!
解决方案
鉴于您在实体上有一个复合 id 定义,您必须使用该CompositeId
方法对其进行映射。在您的子实体中,您还必须为父实体的键列定义引用,因为它是一个复合 ID(这就是我个人不喜欢使用复合 ID 的原因)。
考虑到您的模型:
public class ParentEntity
{
public virtual int Vendor { get; set; }
public virtual int Invoice { get; set; }
// other properties
}
public class ChildEntity
{
public virtual int Vendor { get; set; }
public virtual int Invoice { get; set; }
public virtual int Num { get; set; }
public virtual ParentEntity ParentEntity { get; set; }
}
您的映射可能是这样的:
public class ParentEntityMap : ClassMap<ParentEntity>
{
public ParentEntityMap()
{
CompositeId()
.KeyProperty(x => x.Vendor, "VENDOR")
.KeyProperty(x => x.Invoice, "INVOICE");
// other mappings ...
}
}
public class ChildEntityMap : ClassMap<ChildEntity>
{
public ChildEntity()
{
CompositeId()
.KeyProperty(x => x.Vendor, "VENDOR")
.KeyProperty(x => x.Invoice, "INVOICE")
.KeyProperty(x => x.Num, "NUM");
References(x => x.ParentEntity)
.Columns("VENDOR", "INVOICE")
.ReadOnly(); // if you are mapping the same columns of compositeId here, define it as readOnly to avoid inserting data two times on the same columns.
}
}
我没有尝试此代码,这只是一个建议,以检查哪些对您有用。
推荐阅读
- python-3.x - 使用 Nd 数组切片 N+1d Numpy 数组
- javascript - 在 VueJS 中输出动态值?
- google-drive-api - 尽管文件存在于谷歌驱动器上,但谷歌驱动器 API v3 找不到文件
- swagger - 如何引用与属性相同的架构?
- google-maps - “预期类型”的含义?
- c# - 模拟对象中的方法不是从 sut 调用的
- verilog - Verilog 编译错误(未声明的标识符)
- java - 从对象的 ArrayList 访问方法
- javascript - 如何获取不属于猫鼬模式的字段
- css - 如果我使用的是 font-awesome NPM 包,如何将字体真棒图标放入 css 内容属性?