Fluent Nhibernate 中的自定义参考映射

发布于 2024-11-18 12:18:23 字数 1047 浏览 3 评论 0原文

在我的数据库中,我有两个表,一个在逻辑上引用另一个表,但没有定义外键(我无法控制数据库,因此必须接受这一点)。例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撕心裂肺的伤痛 2024-11-25 12:18:23

您需要映射 Col1 & Col2 作为复合键,如下所示:

public class Table1Map : ClassMap<Table1>
{
    public Table1Map()
    {
        UseCompositeId()  
                .WithKeyProperty( x => x.Column1 , "Column1" )  
                .WithKeyProperty( x => x.Column2 , "Column2" );  
        HasMany(x => x.Table2s)
                 .WithKeyColumn( "Column1FromTable1" )  
                 .WithKeyColumn( "Column2FromTable1" ) ;
        Map(x => x.Id).Column("Table1Id");
    }
}

Table2 的映射中:

public class Table2Map : ClassMap<Table2>
{
    public Table2Map()
    {
        Id(x => x.Id).Column("Table2Id");
        References(x => x.Table1)
                 .WithColumns( "Column1FromTable1", "Column2FromTable1" ) ;
    }
}

You will need to map Col1 & Col2 as a composite key like that:

public class Table1Map : ClassMap<Table1>
{
    public Table1Map()
    {
        UseCompositeId()  
                .WithKeyProperty( x => x.Column1 , "Column1" )  
                .WithKeyProperty( x => x.Column2 , "Column2" );  
        HasMany(x => x.Table2s)
                 .WithKeyColumn( "Column1FromTable1" )  
                 .WithKeyColumn( "Column2FromTable1" ) ;
        Map(x => x.Id).Column("Table1Id");
    }
}

and in the mapping for Table2:

public class Table2Map : ClassMap<Table2>
{
    public Table2Map()
    {
        Id(x => x.Id).Column("Table2Id");
        References(x => x.Table1)
                 .WithColumns( "Column1FromTable1", "Column2FromTable1" ) ;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文