实体框架6 DBContext只有所有表的一个子集 [英] Entity Framework 6 DBContext with only a subset of all tables

查看:105
本文介绍了实体框架6 DBContext只有所有表的一个子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个巨大的数据库,有770个表,想用EF 6.1x进行一些性能测试。

We have a huge database with 770 tables and want to do some performance testing with EF 6.1x.

我们只想查询770表。是否可以创建一个只有5-6个实体/ DBSets的轻DBContext,而不是使用完整的770表上下文?

当我们使用完整的上下文,使用4个连接的简单查询需要45秒。这太44秒太久了
我们正在使用代码优先(反向工程)。

When we use the full context, a simple query with 4 joins takes 45 seconds. Thats' 44 seconds too long. We are using code-first (reverse engineered).

问题:
当我们创建这样一个光版本的完整上下文(即5表),EF抱怨所有其他与这5个表相关的其他实体缺少密钥。我们只映射这5个表的键,属性,关系,而不是其余的。

The problem: When we create such a "light" version of the full context (i.e. 5 tables only), EF complains that all the other entities that are somehow related to these 5 tables have missing keys. We only map the keys, properties, relationships for those 5 tables, but not the rest.

由于LINQ中编写的查询只能查询5个表,所以EF应该简单地忽略其他765个表,但不会。
为什么不这样? LazyLoading = true / false似乎没有任何影响。

Since the query written in LINQ only queries 5 tables, EF should simply ignore the other 765 tables, but it won't. Why not? LazyLoading=true/false doesn't seem to have any bearing on this.

注意:显然,可以在DB中创建一个视图,使用LINQ查询执行代码中的操作。问题是可以用如上所述的轻DbContext来完成。

Note: Obviously one could create a view in the DB that does what we do in code with a LINQ query. The question is can it be done with a "light" DbContext as above.

上下文中有轻版本:

public class ItemLookupContext : DbContext
{
    static ItemLookupContext()
    {
        Database.SetInitializer<ItemLookupContext>( null );
    }

    public ItemLookupContext()
        : base( "Name=ItemLookupContext" )
    {
        //Configuration.LazyLoadingEnabled = true;
    }

    public DbSet<Identity> Identities { get; set; }
    public DbSet<Item> Items { get; set; }
    public DbSet<Price> Prices { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Brand> Brands { get; set; }

    protected override void OnModelCreating( DbModelBuilder modelBuilder )
    {
        modelBuilder.Configurations.Add( new IdentityMap() );
        modelBuilder.Configurations.Add( new ItemMap() );
        modelBuilder.Configurations.Add( new PriceMap() );
        modelBuilder.Configurations.Add( new DepartmentMap() );
        modelBuilder.Configurations.Add( new BrandMap() );

        //ignore certain entitities to speed up loading?
        //does not work
        modelBuilder.Ignore<...>();
        modelBuilder.Ignore<...>();
        modelBuilder.Ignore<...>();
        modelBuilder.Ignore<...>();
        modelBuilder.Ignore<...>();
    }
}


推荐答案

您尝试像DDD模式之一的有界上下文一样。

what you trying to something like "Bounded Context" which is one of DDD patterns

所以,您可以检查Julia Lerman的这篇文章,收缩具有DDD有界上下文的EF模型

So, you can check this article by Julia Lerman, Shrink EF Models with DDD Bounded Contexts

这篇关于实体框架6 DBContext只有所有表的一个子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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