nhibernate 是否可以通过 ID 添加子对象
nhibernate is it possible to add child object by ID
鉴于 类
public class Board {
public int Id { get; set; }
public int Player1Id { get; set; }
public Player player1 { get; set; }
}
和
public class Player {
public int Id { get; set; }
public string Name { get; set; }
}
在 EF 中,如果我想将一个玩家(已经在数据库中)分配给一个面板,我们可以这样做:
var board = new Board {
Player1Id = 2 //given that the player ID in the db is 2
}
这将创建关联,而无需将玩家实体从数据库中取出并直接分配给它。因此,下次获取棋盘和播放器时,它将 return 来自数据库的播放器对象。
是否可以用 NHibnerate 做同样的事情?
最好的方法(经过所有尝试).. 将使用 Load<TEntity>(id)
.
小引用:
...Load()
returns an object that is an uninitialized proxy and
does not actually hit the database until you invoke a method of the object. This behaviour is very useful if you wish to create an
association to an object without actually loading it from the
database...
简而言之:这是将 ID 转换为 "proxy instance" 并提供映射引用的本机 NHibernate 支持。
可能会有一些自定义封装。例如上层(业务层)IReadFacade<out TEntity>
和一些 LoadById(id)
...
同时检查:
鉴于 类
public class Board {
public int Id { get; set; }
public int Player1Id { get; set; }
public Player player1 { get; set; }
}
和
public class Player {
public int Id { get; set; }
public string Name { get; set; }
}
在 EF 中,如果我想将一个玩家(已经在数据库中)分配给一个面板,我们可以这样做:
var board = new Board {
Player1Id = 2 //given that the player ID in the db is 2
}
这将创建关联,而无需将玩家实体从数据库中取出并直接分配给它。因此,下次获取棋盘和播放器时,它将 return 来自数据库的播放器对象。
是否可以用 NHibnerate 做同样的事情?
最好的方法(经过所有尝试).. 将使用 Load<TEntity>(id)
.
小引用:
...
Load()
returns an object that is an uninitialized proxy and does not actually hit the database until you invoke a method of the object. This behaviour is very useful if you wish to create an association to an object without actually loading it from the database...
简而言之:这是将 ID 转换为 "proxy instance" 并提供映射引用的本机 NHibernate 支持。
可能会有一些自定义封装。例如上层(业务层)IReadFacade<out TEntity>
和一些 LoadById(id)
...
同时检查: