NHibernate - 为什么 NHibernate 不以一对多方式插入子实体?
在我的示例中,我有服务器,并且这些服务器属于服务器组。我正在用服务器填充服务器组并保存服务器组。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我明白了。我需要在 ServerGroup 上指定它应该级联。
I got if figured out. I needed to specify on the ServerGroup that it should Cascade.