什么是NHibernate session.Load在Entity Framework中的等价物?

时间:2011-07-15 12:14:18

标签: nhibernate entity-framework-4.1

以下NHibernate延迟加载与Entity Framework等效的是什么?

product.Categories.Add(s.Load<Category>(cat));

我尝试了这个,但它从数据库中读取了Category表:

product.Categories.Add(db.Categories.Find(cat));

3 个答案:

答案 0 :(得分:9)

没有等价物,因为EF在延迟加载方面存在基本的设计缺陷,因此可能永远不会存在。

我刚才在MS'论坛上问过你的确切问题:http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/fccfcf68-2b53-407f-9a87-a32426db6f36

答案 1 :(得分:3)

以下是我为模拟NHibernate的Load而创建的一些扩展方法。第二个是具有复合键的实体。使用起来比第一个更难听,但它确实有效。

compile 'com.github.bumptech.glide:glide:3.7.0'

Glide.with(context).load(url).into(imageView);

使用示例:

public static class EntityFrameworkExtensions
{
    // Loads when the Id property is always "Id" based on derived types of EntityBase<TId>
    public static TEntity LoadEntity<TEntity,TId>(this DbContext context, TId id) where TEntity : EntityBase<TId>, new()
    {
        var entity = context.ChangeTracker.Entries<TEntity>().SingleOrDefault(e => e.Entity.Id.Equals(id))?.Entity;

        if (entity == null)
        {
            entity = new TEntity { Id = id };
            context.Set<TEntity>().Attach(entity);
        }

        return entity;
    }

    // Loads when you're dealing with a composite key and need to specify both how to identify the key and how to assign if attaching a newly created entity.
    public static TEntity LoadEntity<TEntity>(this DbContext context, Func<TEntity, bool> predicate, Action<TEntity> idAssignmentAction) where TEntity : class, new()
    {
        var entity = context.ChangeTracker.Entries<TEntity>().SingleOrDefault(e => predicate(e.Entity))?.Entity;

        if (entity == null)
        {
            entity = new TEntity();
            idAssignmentAction(entity);
            context.Set<TEntity>().Attach(entity);
        }

        return entity;
    }
}

    // Loads by allowing you to specify an expression identifying the primary key property
    public static TEntity LoadEntity<TEntity, TIdProperty>(this DbContext context,
        Expression<Func<TEntity, TIdProperty>> idExpression, TIdProperty id) where TEntity : class, new()
    {
        var parameterExpression = Expression.Parameter(typeof(DbEntityEntry<TEntity>), "ent");
        Expression entityProperty = Expression.Property(parameterExpression, "Entity");
        var keyValue = Expression.Invoke(idExpression, entityProperty);
        var pkValue = Expression.Constant(id, typeof(TIdProperty));
        Expression equalsExpression = Expression.Equal(keyValue, pkValue);
        var lambda = Expression.Lambda<Func<DbEntityEntry<TEntity>, bool>>(equalsExpression, parameterExpression);
        var lambdaC = lambda.Compile();

        var entity = context.ChangeTracker.Entries<TEntity>().SingleOrDefault(lambdaC)?.Entity;


        if (entity == null)
        {
            entity = new TEntity();

            var valueParameterExpression = Expression.Parameter(typeof(object));
            var targetExpression = idExpression.Body is UnaryExpression
                ? ((UnaryExpression) idExpression.Body).Operand
                : idExpression.Body;

            var assign = Expression.Lambda<Action<TEntity, object>>
            (
                Expression.Assign(targetExpression,
                    Expression.Convert(valueParameterExpression, targetExpression.Type)),
                idExpression.Parameters.Single(),
                valueParameterExpression
            );

            assign.Compile().Invoke(entity, id);
            context.Set<TEntity>().Attach(entity);
        }

        return entity;
    }

答案 2 :(得分:0)

目前没有相应的内容。

您可以试试这个,假设您不会对该类别进行任何更改。

实体框架4.0:

Category cat = new Category();
cat.Id = i;
context.Attach("Categories", cat);
product.Categories.Add(cat);

实体框架4.1:

Category cat = new Category();
cat.Id = i;
context.Categories.Attach(cat);
product.Categories.Add(cat);

MSDN link