如何在 Fluent NHibernate 中映射 SubclassMap 和 HasManyToMany

发布于 2024-08-31 07:05:01 字数 1248 浏览 7 评论 0原文

我的问题是流畅的 nhibernate 映射多对多关系,它们最终引用了一个不存在的 Id。

public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Password);
        Map(x => x.Confirmed);
        HasMany(x => x.Nodes).Cascade.SaveUpdate();
        HasManyToMany<Node>(x => x.Events).Cascade.SaveUpdate().Table("RSVPs");
    }

public EventMap()
    {

        Map(x => x.Starts);
        Map(x => x.Ends);
        HasManyToMany<User>(x => x.Rsvps).Cascade.SaveUpdate().Table("RSVPs");
    }

public NodeMap() {
        Id(x => x.Id);

        Map(x => x.Title);
        Map(x => x.Body).CustomSqlType("text");
        Map(x => x.CreationDate);
        References(x => x.Author).Cascade.SaveUpdate();
        Map(x => x.Permalink).Unique().Not.Nullable();

    }

这些是我的类 - 请注意 Event 继承自 Node:

public class Event : Node//, IEvent
{
    private DateTime _starts = DateTime.MinValue;
    private DateTime _ends = DateTime.MaxValue;
    public virtual IList<User> Rsvps { get; set; }

}

问题是,生成的 RSVP 表如下所示:

Event_id 用户身份 Node_id

当然,事件表没有 ID - 只有 Node_id。

当尝试保存关系时,它将尝试保存 NULL event_id,从而生成错误。

My problem is fluent nhibernate mapping a many to many relationship, they end up referencing a non existent Id.

public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Password);
        Map(x => x.Confirmed);
        HasMany(x => x.Nodes).Cascade.SaveUpdate();
        HasManyToMany<Node>(x => x.Events).Cascade.SaveUpdate().Table("RSVPs");
    }

public EventMap()
    {

        Map(x => x.Starts);
        Map(x => x.Ends);
        HasManyToMany<User>(x => x.Rsvps).Cascade.SaveUpdate().Table("RSVPs");
    }

public NodeMap() {
        Id(x => x.Id);

        Map(x => x.Title);
        Map(x => x.Body).CustomSqlType("text");
        Map(x => x.CreationDate);
        References(x => x.Author).Cascade.SaveUpdate();
        Map(x => x.Permalink).Unique().Not.Nullable();

    }

Those are my classes -notice that Event inherits from Node:

public class Event : Node//, IEvent
{
    private DateTime _starts = DateTime.MinValue;
    private DateTime _ends = DateTime.MaxValue;
    public virtual IList<User> Rsvps { get; set; }

}

The problem is, the generated RSVPs table is like that:

Event_id
User_id
Node_id

Of course the Event table has no ID - only a Node_id.

When trying to save a relationship it will try to save a NULL event_id thus generating an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

幸福丶如此 2024-09-07 07:05:01

好吧,我应该更了解流畅的 NHibernate:经过一些尝试和错误,一切正常。但是,我应该加深我的知识。这是工作代码:(

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Password);
        Map(x => x.Confirmed);
        HasMany(x => x.Nodes).Cascade.SaveUpdate();
        HasManyToMany<Node>(x => x.Events)
            .Table("RSVPs")
            .ParentKeyColumn("User_id")
            .ChildKeyColumn("Event_id");
    }
}

public EventMap()
    {

        Map(x => x.Starts);
        Map(x => x.Ends);
        HasManyToMany(x => x.Rsvps)
            .Cascade.SaveUpdate()
            .Table("RSVPs")
            .Inverse()
            .ParentKeyColumn("Event_id")
            .ChildKeyColumn("User_id");
    }

public NodeMap() 
{
        Id(x => x.Id);

        Map(x => x.Title);
        Map(x => x.Body).CustomSqlType("text");
        Map(x => x.CreationDate);
        References(x => x.Author).Cascade.SaveUpdate();
        Map(x => x.Permalink).Unique().Not.Nullable();
    }

节点和事件类保持不变)

我必须指定:

  1. 表的名称
  2. ParentKeyColumn 和 ChildKEyColumn 的名称(注意它们在两个类中是逆的)
  3. 映射上的逆

EventMap Hope 这可以帮助某人作为 HasManyToMany / SubclassMap 示例。

Ok, I should have known fluent NHibernate better: after some trial and error, everything works. However, I should deepen my knowledge. This is the working code:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Password);
        Map(x => x.Confirmed);
        HasMany(x => x.Nodes).Cascade.SaveUpdate();
        HasManyToMany<Node>(x => x.Events)
            .Table("RSVPs")
            .ParentKeyColumn("User_id")
            .ChildKeyColumn("Event_id");
    }
}

public EventMap()
    {

        Map(x => x.Starts);
        Map(x => x.Ends);
        HasManyToMany(x => x.Rsvps)
            .Cascade.SaveUpdate()
            .Table("RSVPs")
            .Inverse()
            .ParentKeyColumn("Event_id")
            .ChildKeyColumn("User_id");
    }

public NodeMap() 
{
        Id(x => x.Id);

        Map(x => x.Title);
        Map(x => x.Body).CustomSqlType("text");
        Map(x => x.CreationDate);
        References(x => x.Author).Cascade.SaveUpdate();
        Map(x => x.Permalink).Unique().Not.Nullable();
    }

(The node and event class remain the same)

I had to specify:

  1. The name of the table
  2. The name of ParentKeyColumn and ChildKEyColumn (notice they are the inverse in the two classes)
  3. Inverse on the mapping of EventMap

Hope this can help someone as a HasManyToMany / SubclassMap example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文