EF Core 2.1 启动缓慢 [英] EF Core 2.1 slow startup

查看:32
本文介绍了EF Core 2.1 启动缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对 EF6 有了一些经验之后(比如这个),我想要尝试使用 EF Core,因为我阅读了一些文章并观看了一些视频,说它在很大程度上优于 EF6.

After having some experience with EF6 (like this), I wanted to try out EF Core, because I've read some articles and watched some videos saying that it outperforms EF6 by a very large margin.

所以,我用类制作了示例程序:

So, I made sample program with class:

 public interface IEntity
    {
        int Id { get; set; }
    }     

public class Employee : IEntity
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
    }

然后我用通用存储库创建了一个存储库模式:

Then I created a repository pattern with generic repository:

public interface IRepository<T> : IDisposable where T : IEntity
{
    void Insert(T entity);
    void Delete(T entity);
    IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
    IQueryable<T> GetAll();
    T GetById(int id);
    void Update(T entity);
    void BeginTransaction();
    IDbContextTransaction Transaction { get; }
}

public class Repository<T> : IRepository<T> where T : class, IEntity
{
    private RosterContext _context;
    private IDbContextTransaction _transaction;

    public Repository(bool wrapTransaction = false)
    {
        _context = new MyContext();

        if (wrapTransaction)
        {
            _transaction = _context.Database.BeginTransaction();
        }
    }

    public void Update(T entity)
    {
        _context.Set<T>().Update(entity);
        _context.SaveChanges();
    }

    public void BeginTransaction()
    {
        if (_transaction == null)
            _transaction = _context.Database.BeginTransaction();
    }

    public void Insert(T entity)
    {
        _context.Set<T>().Add(entity);
        _context.SaveChanges();
    }

    public void Delete(T entity)
    {
        var toDelete = GetById(entity.Id);
        _context.Set<T>().Remove(toDelete);
        _context.SaveChanges();
    }

    public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
    {
        return _context.Set<T>().AsNoTracking().Where(predicate);
    }

    public IQueryable<T> GetAll()
    {
        return _context.Set<T>().AsNoTracking();
    }

    public T GetById(int id)
    {
        return _context.Set<T>().AsNoTracking().FirstOrDefault(x => x.Id == id);
    }


    public IDbContextTransaction Transaction => _transaction;

    public void Dispose()
    {
        _transaction?.Dispose();
        _context?.Dispose();
    }
}

这是上下文:

    public class MyContext : DbContext
    {
        public DbSet<Employee> Employees { get; set; }

        public MyContext()
        {
            Database.Migrate();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=test.db");
        }
    }

如您所见,这是最简单的示例:一张表,一个实体,其中存储在 SQLite 中的 2 个属性.问题是,第一次查询需要将近 5 秒,数据库中有 10 行.下一个是即时的.

As you can see, this is the simplest sample that there can be: one table, one entity with 2 properties stored in SQLite. The problem is, that first query takes almost 5 seconds with 10 rows in database. Next ones are instant.

我在带有 SSD 驱动器和 i5 处理器的计算机上工作.

I work on computer with SSD drive and i5 processor.

有什么问题?是 SQLite 吗?是 Database.Migrate(); (如果我评论这一行,它不会改变任何东西)?或者所有的性能改进都很糟糕?

What is the problem? Is it SQLite? Is it Database.Migrate(); (if I comment this line it does not change anything)? Or all performance improvements are just bad?

推荐答案

其实这个问题只有在调试的时候才会出现.原因是实体框架抛出并捕获了数千个异常,它们减慢了调试器的速度.请在此处查看 GitHub 问题.

Actually the problem occurs only while debugging. The cause is that Entity Framework throws and catches thousands of exceptions, and they slow down the debugger. See the GitHub issue here.

为了解决这个问题,我在工具 -> 选项 -> 调试 -> 常规中启用了仅启用我的代码"选项.这样 Visual Studio 就不会跟踪来自实体框架的异常.

To solve it I enabled "Enable Just My Code" option in Tools -> Options -> Debugging -> General. That way Visual Studio does not trace the exceptions from Entity Framework.

这篇关于EF Core 2.1 启动缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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