gpt4 book ai didi

c# - Fluent NHibernate - HasMany 复合键

转载 作者:太空狗 更新时间:2023-10-29 21:45:00 31 4
gpt4 key购买 nike

如何在没有外键且列名不同的旧数据库上创建 HasMany 关系?我知道如何使用引用在 HeaderVersion 上创建一对一关系,但不知道如何创建从 Header 1 到 * HeaderVersion 的 HasMany。加入条件应该是:

Header.Id1 = HeaderVersion.PId1 AND Header.Id2 = HeaderVersion.PId2

Model

域:

public class Header {
public virtual int Id1 { get; set; }
public virtual int Id2 { get; set; }
public virtual string Something { get; set; }
}

public class HeaderVersion {
public virtual int PId1 { get; set; }
public virtual int PId2 { get; set; }
public virtual int Version { get; set; }
public virtual string SomethingFoo { get; set; }
}

map :

 public HeaderMap() {
Table("Header");
LazyLoad();
CompositeId().KeyProperty(x => x.Id1, "Id1")
.KeyProperty(x => x.Id2, "Id2");
Map(x => x.Something).Column("Something");
}

public HeaderVersionMap() {
Table("HeaderVersion");
LazyLoad();
CompositeId().KeyProperty(x => x.PId1, "PId1")
.KeyProperty(x => x.PId2, "PId2")
.KeyProperty(x => x.Version, "Version");
Map(x => x.SomethingFoo).Column("SomethingFoo");
}

最佳答案

假设 Header 是(省略了 GetHashCodeEquals 的覆盖)

public class Header
{
public virtual int Id1 { get; set; }
public virtual int Id2 { get; set; }
public virtual string Something { get; set; }
public virtual ICollection<HeaderVersion> Versions { get; set; }
}

HeaderMap 应该看起来像:

public class HeaderMap : ClassMap<Header>
{
public HeaderMap()
{
Table("Header");
LazyLoad();
CompositeId().KeyProperty(x => x.Id1, "Id1")
.KeyProperty(x => x.Id2, "Id2");
Map(x => x.Something).Column("Something");
HasMany(x => x.Versions).AsBag().KeyColumns.Add("PId1", "PId2");
// Fetch, Inverse, Cascade depend on your mapping strategy
}
}

关于c# - Fluent NHibernate - HasMany 复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19399882/

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