Fluent Nhibernate 中的自定义参考映射
在我的数据库中,我有两个表,一个在逻辑上引用另一个表,但没有定义外键(我无法控制数据库,因此必须接受这一点)。例如:
Table1 (
Table1Id int,
Column1 int,
Column2 int
)
Table2 (
Table2Id int,
Column1FromTable1 int,
Column2FromTable1 int
)
假设保证 (Column1, Column2)
对对于 Table1
是唯一的。
在代码中,我想为 Table1
定义 HasMany
映射,如下所示:
public class Table1
{
public int Id
{
get;
set;
}
public IEnumerable<Table2> Table2s
{
get;
set;
}
}
public class Table2
{
public int Id
{
}
public Table1 Table1
{
get;
set;
}
}
public class Table1Map : ClassMap<Table1>
{
public Table1Map()
{
Id(x => x.Id).Column("Table1Id");
HasMany(x => x.Table2s); //What next?
}
}
我需要为此做什么?我可以使用 OneToManyPart
的哪些方法来定义引用?
简单来说,使用 Fluent NHibernate,如何绑定未绑定在数据库中的代码实体?问题还在于我必须在这里使用的复杂密钥。
同样,在这种情况下我无法更改数据库,否则这将是我的自然选择。
In my database, I have two tables, one logically referencing other, but with no foreign key defined (I cannot control the database, so have to live with that). E.g.:
Table1 (
Table1Id int,
Column1 int,
Column2 int
)
Table2 (
Table2Id int,
Column1FromTable1 int,
Column2FromTable1 int
)
Suppose there is guarantee that (Column1, Column2)
pair is unique for Table1
.
In the code, I want to define HasMany
mapping for Table1
, like this:
public class Table1
{
public int Id
{
get;
set;
}
public IEnumerable<Table2> Table2s
{
get;
set;
}
}
public class Table2
{
public int Id
{
}
public Table1 Table1
{
get;
set;
}
}
public class Table1Map : ClassMap<Table1>
{
public Table1Map()
{
Id(x => x.Id).Column("Table1Id");
HasMany(x => x.Table2s); //What next?
}
}
What do I have to do for that? What methods of OneToManyPart
can I use to define the reference?
In simple words, with Fluent NHibernate, how do I tie in the code entities that are not tied in the database? The problem is also in the complex key that I have to use here.
Again, I cannot change the database in this case, which would be my natural choice otherwise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要映射
Col1
&Col2
作为复合键,如下所示:在
Table2
的映射中:You will need to map
Col1
&Col2
as a composite key like that:and in the mapping for
Table2
: