中间表上的Nhibernate ComposedId [英] Nhibernate ComposedId on intermediate table

查看:108
本文介绍了中间表上的Nhibernate ComposedId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个只有两个外键(作为ComposedId)的中间表. 但是NHibernate会自动创建一个"id"属性.

I want to have a intermediate table with only two foreign keys (as a ComposedId). But NHibernate is automatically creating a "id" property.

我有以下课程

public class Lace
{
    public virtual int Id { get; set; }
    public virtual string Hostname { get; set; }

    public virtual IList<LaceHasCard> LaceHasCards { get; set; }
}

public class Card
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<LaceHasCard> LaceHasCards { get; set; }
}

和这个手动创建的中间表

and this manually created intermediate table

public class LaceHasCard
{
    public virtual Card Card { get; set; }
    public virtual Lace Lace { get; set; }
}

映射

public LaceMapping()
{
    Id(x => x.Id, map => map.Generator(Generators.Native));
    Property(x => x.Hostname);

    Bag(x => x.LaceHasCards, col =>
    {
        col.Key(k => k.Column("LaceId"));
        col.Inverse(true);
    }, r => r.OneToMany());
}

public CardMapping()
{
    Id(x => x.Id, map => map.Generator(Generators.Native));
    Property(x => x.Name);

    Bag(x => x.LaceHasCards, col =>
    {
        col.Key(k => k.Column("CardId"));
        col.Inverse(true);
    }, r => r.OneToMany());
}

中间表映射

public LaceHasCardMapping()
{
    //ComposedId(map =>
    //{
    //    map.Property(x => x.Card.Id, a =>
    //    {
    //        a.Column("CardId");
    //    });
    //    map.Property(x => x.Lace.Id, a =>
    //    {
    //        a.Column("LaceId");
    //    });
    //});

    ManyToOne(x => x.Card, map =>
    {
        map.Column("CardId");
    });

    ManyToOne(x => x.Lace, map =>
    {
        map.Column("LaceId");
    });
}

如果我在注释掉ComposedId的情况下创建模式,则NHibernate将在表中创建一个"id"属性.

If I create the schema with the ComposedId commented out, NHibernate will create a "id" property in the table.

CREATE TABLE [dbo].[LaceHasCard] (
    [id]     INT NOT NULL,
    [CardId] INT NULL,
    [LaceId] INT NULL,
    PRIMARY KEY CLUSTERED ([id] ASC),
    CONSTRAINT [FKDC6D54711CD160AE] FOREIGN KEY ([CardId]) REFERENCES [dbo].[Card] ([Id]),
    CONSTRAINT [FKDC6D547151F8AF85] FOREIGN KEY ([LaceId]) REFERENCES [dbo].[Lace] ([Id])
);

如果尝试使用ComposedId创建架构,则会收到以下错误消息:

If I try to create the schema with the ComposedId, I get the following error message:

无法实例化映射类(请参见InnerException): EmpLaceMgmt.Models.Mappings.LaceHasCardMapping

Unable to instantiate mapping class (see InnerException): EmpLaceMgmt.Models.Mappings.LaceHasCardMapping

告诉NHibernate创建组合ID的正确方法是什么?

What would be the right way to tell NHibernate to create a composed Id?

推荐答案

让我给您建议,只是我的观点-不要使用复合ID.在数据库中使用标准主键及其C#/实体表示形式为Id { get; set; }

Let me give you suggestion, just my point of view - do not use composite id. Use standard primary key in DB and its C# / entity representation as Id { get; set; }

...

...

NHibernate使标识符属性为可选.出于各种原因,您应该使用它们. 我们建议标识符是合成的"(生成的,没有任何业务意义)并且非原始类型.为了获得最大的灵活性,请使用Int64或String.

NHibernate makes identifier properties optional. There are all sorts of reasons why you should use them. We recommend that identifiers be 'synthetic' (generated, with no business meaning) and of a non-primitive type. For maximum flexibility, use Int64 or String.

另请参阅Wiki上有关合成,替代密钥的更多信息.

See also more about synthetic, surrogate keys at wiki.

根据我的经验,我们不必担心会有这样的配对对象:

From my experience, we should not be worry about having pairing object like this:

public class LaceHasCard
{
    public virtual int Id { get; set; } // the key
    public virtual Card Card { get; set; }
    public virtual Lace Lace { get; set; }
}

因为稍后它将变得非常容易访问:

Because later it would become so easy to access it:

session.Get<LaceHasCard>(id)

并且还可以在Subqueries 中使用它(用于过滤带有花边的Card,反之亦然)

数据库中的一个自动生成的列应该不会造成任何额外的负面影响.但是处理这样的表要容易(很多)...

One column in DB, autogenerated, should not have any extra bad impact. But handling such table is a bit (a lot) easier...

总而言之,我的建议是,使所有实体成为具有完全权限(包括合成/代理密钥)的第一级公民

So, summary, my suggestion would be, make all entities first level citizens, with full rights (including synthetic/surrogate key)

这篇关于中间表上的Nhibernate ComposedId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆