Entity Framework Code-First非常慢 [英] Entity Framework Code-First is very slow

查看:92
本文介绍了Entity Framework Code-First非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从广泛的谷歌搜索看来,我不是遇到此问题的第一人,但是我一直找不到能令人满意地解决该问题的人-我正在与旧数据库集成, (目前)仅尝试与单个表集成,但是我对该模型的第一个查询需要大约12秒钟的时间才能执行。正如预期的那样,第二个调用几乎是即时的。

From extensive googling, it would appear that I'm not the first person to encounter this problem, but I have been unable to find anyone who was able to satisfactorily solve it - I am integrating with a legacy database, and I am only attempting to integrate with a single table (at the moment) and yet my first query to this model takes about 12 seconds or so to execute. The second call is almost instant though, as expected.

接下来是我的实体框架代码的全部第一次设置:

What follows is the entirety of my Entity Framework Code First setup:

public class PortalDatabase : DbContext
{
    public DbSet<User> Users { get; set; }

    public PortalDatabase():base("portalDatabase")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        User.ConfigureEntity(modelBuilder.Entity<User>());
    }
}

public class User
{
    public int ID { get; set; }

    public string FullName { get; set; }

    /// <summary>
    /// Internal representation of the IsDisabled flag. This should not be
    /// written to; use <see cref="IsDisabled"/> instead.
    /// </summary>
    internal byte IsDisabledInternal { get; set; }

    public bool IsDisabled
    {
        get { return Convert.ToBoolean(this.IsDisabledInternal); }
        set { this.IsDisabledInternal = Convert.ToByte(value); }
    }

    public int LoginAttempts { get; set; }

    public string EmailAddress { get; set; }

    public string Password { get; set; }

    internal static void ConfigureEntity(EntityTypeConfiguration<User> entity)
    {
        entity.ToTable("Users");

        entity.Property(model => model.ID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("UserID");

        entity.Property(model => model.FullName)
            .HasColumnName("UserName");

        entity.Property(model => model.IsDisabledInternal)
            .HasColumnName("AccountLocked");

        entity.Ignore(model => model.IsDisabled);

        entity.Property(model => model.LoginAttempts)
            .HasColumnName("LoginAttempts");

        entity.Property(model => model.EmailAddress)
            .HasColumnName("EmailAddress");

        entity.Property(model => model.Password)
            .HasColumnName("Password");
    }
}

此代码示例需要8到12秒的时间来执行:

This code sample takes 8-12 seconds to execute:

    PortalDatabase database = new PortalDatabase();

    IEnumerable<User> users = from user in database.Users
                              where user.ID == 66
                              select user;

我知道此问题与元数据生成有关,元数据生成仅执行一次,而根据ScottGuide的评论大大提高了性能:

I understand that this issue is to do with metadata generation, which is done only once, which according a comment from ScottGu "improves performance a lot":


代码优先库使用与传统库相同的底层EF方法-因此性能特征应大致相同。 代码优先库还包括一些智能工具,因此可以缓存为映射到数据库或从数据库映射而检索到的元数据-只需计算一次即可(大大提高了性能)。

The "code first" library uses the same underlying EF as the traditional approach - so the performance characteristics should be about the same. The "code first" library also includes aome smarts so that the meta-data that is retrieved for mapping to/from the database is cached - so that it only needs to be calculated once (which improves performance a lot).

但是我所描述的性能是平均水平吗?我无法想象花费12秒来执行一个简单的查询对Entity Framework团队来说是可以接受的。我误会他了吗?元数据缓存是否在例如IIS应用程序池的生存期内一直存在?

But is the performance I have described average? I can't imagine that taking 12 seconds to execute a simple query would ever be acceptable to the Entity Framework team. Am I misunderstanding him? Does the metadata cache persist for the lifetime of, for example, an IIS application pool? That might be in some way acceptable, although still hardly ideal.

如果我不是先使用代码,那么我就可以使用EdmGen.exe生成我的视图,据我所知,这将使我的应用程序速度大大提高。

If I wasn't using code first, then I'd be able to use EdmGen.exe to generate my views, which as I understand it would make my application considerably faster. Is there an equivalent when developing my model using code first?

2012年2月14日更新:感谢Pawel的帖子,我能够发表自己的观点。不幸的是,这并没有改变创建新PortalDatabase实例的速度,这仍然需要相同的时间。我知道正在使用视图,因为我在构造函数中放置了一个断点,但这并不影响任何事情。

Update 14th Feb 2012: Thanks to Pawel's post, I was able to generate my views. Unfortunately, this has not changed the speed of creating a new PortalDatabase instance, which still takes the same amount of time. I know that the views are being used because I put a breakpoint in the constructor, but this doesn't affect anything.

推荐答案

要使用CodeFirst生成视图,请使用EF Power Tools。在此处查看更多详细信息: http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

To generate views with CodeFirst use EF Power Tools. See more details here: http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

这篇关于Entity Framework Code-First非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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