[英]NHibernate IsLoaded and Load for child collections like in Entity Framework
I am busy adapting the Microsoft.AspNet.Identity.EntityFramework
project to instead use NHibernate for the ORM. 我正在忙于修改
Microsoft.AspNet.Identity.EntityFramework
项目,以将NHibernate用于ORM。 It has been fairly easy, with a lot of changes adding async
variants for various EF methods where NH has no built-in async. 这相当容易,通过大量更改为各种EF方法添加了
async
变体,其中NH没有内置异步。
My biggest concern so far is with code like the following for this (much abridged) DbAspNetUsers
class: 到目前为止,我最大的担心是针对此类(非常删节的)
DbAspNetUsers
类的以下代码:
public partial class DbAspNetUsers : IDomainEntity<String>
{
...
public virtual IList<DbAspNetUserClaims> AspNetUserClaimsList{ get; set; }
...
}
Now elsewhere, in the original EF UserStore
class (hence some name differences), and for more properties than just claims, we have code like the following: 现在,在其他地方,在原始的EF
UserStore
类中(因此有一些名称差异),并且除了声明以外,还有更多的属性,我们有如下代码:
private async Task EnsureClaimsLoaded(TUser user)
{
if (!Context.Entry(user).Collection(u => u.Claims).IsLoaded)
{
var userId = user.Id;
await _userClaims.Where(uc => uc.UserId.Equals(userId)).LoadAsync().WithCurrentCulture();
Context.Entry(user).Collection(u => u.Claims).IsLoaded = true;
}
}
The NH XML mapping for claims is as follows: 声明的NH XML映射如下:
<class name="DbAspNetUsers, Core.Data" table="AspNetUsers" lazy='true'>
...
<bag name="AspNetUserClaimsList" inverse="true" cascade="all" lazy='true'>
<key column="UserId" />
<one-to-many class="DbAspNetUserClaims, Core.Data" />
</bag>
...
</class>
Does NHibernate have any functionality to cover scenarios like this, or should I simply write methods that do the work of EF's LoadAsync
, and then set a Loaded
flag, repeated for each child collection? NHibernate是否有任何功能可以覆盖这样的场景,还是我应该简单地编写完成EF的
LoadAsync
的工作的方法,然后设置一个Loaded
标志(对于每个LoadAsync
重复执行)?
Any suggestions, commands, or just advice on how to handle this in NH if I can would be greatly appreciated, as would even being convincingly told I cannot do this and must simply load each entity as needed. 如果可以的话,任何有关如何在NH中进行处理的建议,命令或建议,都将不胜感激,甚至令人信服地告诉我我无法做到这一点,必须根据需要简单地加载每个实体。
Please excuse the terrible naming. 请原谅糟糕的命名。 I'm using the 'official' mappings generator for the office.
我在办公室使用“官方”映射生成器。
NHibernateUtil.IsInitialized(user.Claims)
would give you an idea whether your lazy collection has been loaded. NHibernateUtil.IsInitialized(user.Claims)
将使您了解是否已加载了惰性集合。
As for actually loading them, you could either do NHibernateUtil.Initialize()
or simply start using the collection (eg calling user.Claims.Count()
would cause NH to fetch the collection for you) 至于实际加载它们,您可以执行
NHibernateUtil.Initialize()
或简单地开始使用集合(例如,调用user.Claims.Count()
会导致NH为您获取集合)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.