为什么不在实体框架中加载工作 [英] why isn't eager loading working in Entity Framework

查看:107
本文介绍了为什么不在实体框架中加载工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ProjectVersions列表中进行基本的热心加载,其中每个ProjectVersion都有一个FieldValues和ChildProjects列表。我想要在ProjectVersions被加载的同时加载FieldValues和ChildProjects及其所有的属性,但是在我的代码中,当通过每个ProjectVersion时,它仍然会点击数据库来获取这些集合(检查sql server profiler)。任何指针都会有帮助。

  var publishedList = Repository.Find< Project>().//一堆歌曲并选择

IEnumerable< ProjectVersion> publishedList = published
.Include(x => x.FieldValues)
.Include(x => x.ChildProjects)
.ToList();

//编辑:上下文隐藏在通用存储库后面。以下是一些细节:

public class Repository:IRepository
{
internal readonly IDataContext _context;

public Repository(IDataContext context)
{
_context = context;
_context.Committed + = _context_Committed;
_context.Deleted + = _context_Deleted;
}
public IQueryable< T>查找< T>()其中T:class,IEntity
{
return _context.Repository< T>();
}
}

public class EfDataContext:IDataContext
{
public IQueryable< T>存储库< T>()其中T:class,IEntity
{
var table = _context.Set(typeof(T));
WrappedFieldsObjectQuery< T>(table.Cast< T>()。AsExpandable()));
return table.Cast< T>()。AsExpandable();
}
}

public class MsmDbContext:DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder .Conventions.Remove< PluralizingTableNameConvention>();

var typesToRegister = Assembly.GetExecutingAssembly()。GetTypes()
.Where(type =>
type.IsClass&&
type.BaseType。 IsGenericType&
type.BaseType.GetGenericTypeDefinition()== typeof(EntityTypeConfiguration<>));

foreach(varconfig in typesToRegister.Select(Activator.CreateInstance))
{
modelBuilder.Configurations.Add((dynamic)config);
}

base.OnModelCreating(modelBuilder);
}
}

public class ProjectMapping:EntityTypeConfiguration< Project>
{
public ProjectMapping()
{
HasOptional(p => p.LastChangedBy).WithMany(p => p.ProjectsChanged).WillCascadeOnDelete();
HasRequired(p => p.CreatedBy).WithMany(p => p.ProjectsCreated).WillCascadeOnDelete();
HasRequired(d => d.Account).WithMany(p => p.Projects).WillCascadeOnDelete();
HasRequired(d => d.PinType).WithMany(p => p.Projects).HasForeignKey(p => p.PinType_Id).WillCascadeOnDelete();



public static class RepositoryFactory
{
public static IRepository CreateRepository()
{
return CreateEfRepository() ;
}

内部静态IRepository CreateEfRepository()
{
return new Repository(new EfDataContext(new MsmDbContext()));
}
}


解决方案

通过直接使用DataContext,我得到它的工作。不知何故存储库正在拧紧它。


I'm trying to do basic eager loading on a list of ProjectVersions where each ProjectVersion has a list of FieldValues and ChildProjects. I want the FieldValues and ChildProjects to be loaded along with all their properties when ProjectVersions is loaded, but it seems that in my code when going through each ProjectVersion, it still hits the database to get these collections (checking sql server profiler). Any pointers would be helpful.

    var publishedList = Repository.Find<Project>().//a bunch of wheres and selects

            IEnumerable<ProjectVersion> publishedList = published
                .Include(x => x.FieldValues)
                .Include(x => x.ChildProjects)
                .ToList();

    //EDIT: the context is hidden behind a generic Repository. Below are some details:

        public class Repository : IRepository
            {
                internal readonly IDataContext _context;

                public Repository(IDataContext context)
                {
                    _context = context;
                    _context.Committed += _context_Committed;
                    _context.Deleted += _context_Deleted;
                }
                public IQueryable<T> Find<T>() where T : class, IEntity
                {
                    return _context.Repository<T>();
                }
        }

        public class EfDataContext : IDataContext
            {
                public IQueryable<T> Repository<T>() where T : class, IEntity
                {
                    var table = _context.Set(typeof(T));
                    WrappedFieldsObjectQuery<T>(table.Cast<T>().AsExpandable()));
                    return table.Cast<T>().AsExpandable();
                }
        }

    public class MsmDbContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type =>
                    type.IsClass &&
                    type.BaseType.IsGenericType &&
                    type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));

            foreach (var config in typesToRegister.Select(Activator.CreateInstance))
            {
                modelBuilder.Configurations.Add((dynamic)config);
            }

            base.OnModelCreating(modelBuilder);
        }
    }

    public class ProjectMapping : EntityTypeConfiguration<Project>
    {
        public ProjectMapping()
        {
            HasOptional(p => p.LastChangedBy).WithMany(p => p.ProjectsChanged).WillCascadeOnDelete();
            HasRequired(p => p.CreatedBy).WithMany(p => p.ProjectsCreated).WillCascadeOnDelete();
            HasRequired(d => d.Account).WithMany(p => p.Projects).WillCascadeOnDelete();
            HasRequired(d => d.PinType).WithMany(p => p.Projects).HasForeignKey(p => p.PinType_Id).WillCascadeOnDelete();
        }
    }

    public static class RepositoryFactory
        {
            public static IRepository CreateRepository()
            {
                return CreateEfRepository();
            }

            internal static IRepository CreateEfRepository()
            {
                return new Repository(new EfDataContext(new MsmDbContext()));
            }
        }

解决方案

Actually, I got it to work by just directly working with the DataContext. Somehow the Repository is screwing it up.

这篇关于为什么不在实体框架中加载工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆