views:

29

answers:

1

Hello..

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!

+1  A: 

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();
        }
Nick