NHibernate - 为什么 NHibernate 不以一对多方式插入子实体?

发布于 2024-09-27 05:01:26 字数 1902 浏览 1 评论 0原文

在我的示例中,我有服务器,并且这些服务器属于服务器组。我正在用服务器填充服务器组并保存服务器组。 ServerGroup 表已填充,但服务器未填充。

public ServerGroupMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        HasMany(x => x.ServersInGroup)
            .Inverse();
    }
 public ServerMap()
    {
        Id(x => x.Id);
        Map(x => x.Description);
        Map(x => x.Address);
        Map(x => x.Password);
        Map(x => x.Ports);          
        HasMany(x => x.Connections)
           .Inverse();
        References(x => x.ServerGroup)
            .Not.Nullable();

    }

当我将 ServerGroup 的持久性和实例保存到数据库时,我期望 NHibernate 也会将 ServerGroup 中包含的服务器正确插入到服务器表中。

NHibernate 正在创建我的数据库模式。以下是 Server 和 ServerGroup 对象:

 public class ServerGroup
{
    public virtual int Id { get; private set; }

    public virtual string Name { get; set; }

    public virtual IList<Server> ServersInGroup { get; set; }


    public ServerGroup()
    {
        ServersInGroup = new List<Server>();
    }

    public virtual void AddServer(Server server)
    {
        server.ServerGroup = this;
        ServersInGroup.Add(server);
    }
}


public class Server
{
    public virtual int Id { get; private set; }

    public virtual string Description { get; set; }

    public virtual string Address { get; set; }

    public virtual string Ports { get; set; }

    public virtual string Password { get; set; }       

    public virtual ServerGroup ServerGroup { get; set; }

    public virtual IList<Connection> Connections { get; set; }


    public Server()
    {
        Connections = new List<Connection>();
    }

    public virtual void AddConnection(Connection connection)
    {
        connection.CurrentServer = this;
        Connections.Add(connection);
    }


}

当插入 ServerGroup 时,为什么“ServersInGroup”中的服务器没有持久化到数据库中?谢谢!

In my example I have Servers and those Servers belong to a ServerGroup. I am populating a ServerGroup with Servers and saving the ServerGroup. The ServerGroup table is populated but the Servers are not.

public ServerGroupMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        HasMany(x => x.ServersInGroup)
            .Inverse();
    }
 public ServerMap()
    {
        Id(x => x.Id);
        Map(x => x.Description);
        Map(x => x.Address);
        Map(x => x.Password);
        Map(x => x.Ports);          
        HasMany(x => x.Connections)
           .Inverse();
        References(x => x.ServerGroup)
            .Not.Nullable();

    }

When I save persist and instance of ServerGroup to the database I was expecting that NHibernate would also insert the Servers contained in the ServerGroup into the Server table correctly.

NHibernate is creating my DB schema. Here are the Server and ServerGroup objects:

 public class ServerGroup
{
    public virtual int Id { get; private set; }

    public virtual string Name { get; set; }

    public virtual IList<Server> ServersInGroup { get; set; }


    public ServerGroup()
    {
        ServersInGroup = new List<Server>();
    }

    public virtual void AddServer(Server server)
    {
        server.ServerGroup = this;
        ServersInGroup.Add(server);
    }
}


public class Server
{
    public virtual int Id { get; private set; }

    public virtual string Description { get; set; }

    public virtual string Address { get; set; }

    public virtual string Ports { get; set; }

    public virtual string Password { get; set; }       

    public virtual ServerGroup ServerGroup { get; set; }

    public virtual IList<Connection> Connections { get; set; }


    public Server()
    {
        Connections = new List<Connection>();
    }

    public virtual void AddConnection(Connection connection)
    {
        connection.CurrentServer = this;
        Connections.Add(connection);
    }


}

How come the Servers within 'ServersInGroup' are not persisted to the database when ServerGroup is inserted? Thanks!

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

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

发布评论

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

评论(1

卖梦商人 2024-10-04 05:01:26

我明白了。我需要在 ServerGroup 上指定它应该级联。

 public ServerGroupMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            HasMany(x => x.ServersInGroup)
                .Cascade.All()
                .Inverse();
        }

I got if figured out. I needed to specify on the ServerGroup that it should Cascade.

 public ServerGroupMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            HasMany(x => x.ServersInGroup)
                .Cascade.All()
                .Inverse();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文