0
在我的示例中,我有服務器,這些服務器屬於ServerGroup。我使用服務器填充ServerGroup並保存ServerGroup。 ServerGroup表被填充,但服務器不是。NHibernate - 爲什麼不NHibernate插入一對多的子實體?
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創建我的數據庫模式。下面是服務器和的ServerGroup對象:被插入的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);
}
}
爲什麼「ServersInGroup」不保存到數據庫中的服務器?謝謝!