实体框架 SaveChanges() 第一次调用非常慢

发布于 2024-11-16 14:36:34 字数 1799 浏览 11 评论 0原文

我很高兴这个问题之前已经被提出过几次,但我找不到明确的答案(也许没有!)。

无论如何,标题确实说明了一切。创建一个新的上下文,添加一个新的实体,SaveChanges()需要20秒。在同一上下文中立即添加第二个实体 SaveChanges()。

对此有什么想法吗? :-)

============ 更新 =============

我创建了一个非常简单的应用程序,针对我现有的模型运行来显示问题...

    public void Go()
    {
        ModelContainer context = new ModelContainer(DbHelper.GenerateConnectionString());

        for (int i = 1; i <= 5; i++)
        {
            DateTime start = DateTime.Now;
            Order order = context.Orders.Single(c => c.Reference == "AA05056");
            DateTime end = DateTime.Now;
            double millisecs = (end - start).TotalMilliseconds;
            Console.WriteLine("Query " + i + " = " + millisecs + "ms (" + millisecs / 1000 + "s)");

            start = DateTime.Now;
            order.Note = start.ToLongTimeString();
            context.SaveChanges();
            end = DateTime.Now;
            millisecs = (end - start).TotalMilliseconds;
            Console.WriteLine("SaveChanges " + i + " = " + millisecs + "ms (" + millisecs / 1000 + "s)");

            Thread.Sleep(1000);
        }

        Console.ReadKey();
    }

请不要对我的代码发表评论 - 除非它是无效测试;)

结果是:

查询 1 = 3999.2288ms (3.9992288s)
SaveChanges 1 = 3391.194ms (3.391194s)

查询 2 = 18.001ms (0.018001s)
SaveChanges 2 = 4.0002ms (0.0040002s)

查询 3 = 14.0008ms (0.0140008s)
SaveChanges 3 = 3.0002ms (0.0030002s)

查询 4 ​​= 13.0008ms (0.0130008s)
SaveChanges 4 = 3.0002ms (0.0030002s)

查询 5 = 10.0005ms (0.0100005s)
SaveChanges 5 = 3.0002ms (0.0030002s)

第一个查询需要时间,我认为是视图生成?或者数据库连接?

第一次保存需要近 4 秒,而我的应用程序中更复杂的保存需要超过 20 秒,这是不可接受的。

现在不知道该去哪里:-(

更新...

SQL Profiler 显示第一个查询和更新很快并且与第一个没有什么不同。所以我知道延迟是实体框架所怀疑的。

I appreciate that this issue has been raised a couple of times before, but I can't find a definitive answer (maybe there isn't one!).

Anyway the title tells it all really. Create a new context, add a new entity, SaveChanges() takes 20 seconds. Add second entity in same context, SaveChanges() instant.

Any thoughts on this? :-)

============ UPDATE =============

I've created a very simple app running against my existing model to show the issue...

    public void Go()
    {
        ModelContainer context = new ModelContainer(DbHelper.GenerateConnectionString());

        for (int i = 1; i <= 5; i++)
        {
            DateTime start = DateTime.Now;
            Order order = context.Orders.Single(c => c.Reference == "AA05056");
            DateTime end = DateTime.Now;
            double millisecs = (end - start).TotalMilliseconds;
            Console.WriteLine("Query " + i + " = " + millisecs + "ms (" + millisecs / 1000 + "s)");

            start = DateTime.Now;
            order.Note = start.ToLongTimeString();
            context.SaveChanges();
            end = DateTime.Now;
            millisecs = (end - start).TotalMilliseconds;
            Console.WriteLine("SaveChanges " + i + " = " + millisecs + "ms (" + millisecs / 1000 + "s)");

            Thread.Sleep(1000);
        }

        Console.ReadKey();
    }

Please do not comment on my code - unless it is an invalid test ;)

The results are:

Query 1 = 3999.2288ms (3.9992288s)
SaveChanges 1 = 3391.194ms (3.391194s)

Query 2 = 18.001ms (0.018001s)
SaveChanges 2 = 4.0002ms (0.0040002s)

Query 3 = 14.0008ms (0.0140008s)
SaveChanges 3 = 3.0002ms (0.0030002s)

Query 4 = 13.0008ms (0.0130008s)
SaveChanges 4 = 3.0002ms (0.0030002s)

Query 5 = 10.0005ms (0.0100005s)
SaveChanges 5 = 3.0002ms (0.0030002s)

The first query takes time which I assume is the view generation? Or db connection?

The first save takes nearly 4 seconds which for the more complex save in my app takes over 20 seconds which is not acceptable.

Not sure where to go with this now :-(

UPDATE...

SQL Profiler shows first query and update are fast and are not different for first. So I know delay is Entity Framework as suspected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他不在意 2024-11-23 14:36:34

它可能不是 SaveChanges 调用 - 第一次在 EF 中对数据库进行任何调用时,它必须从元数据生成一些初始代码。您可以在编译时预先生成: http://msdn.microsoft .com/en-us/library/bb896240.aspx

如果这是唯一的问题,我会感到惊讶,但它可能会有所帮助。

另请查看此处: http://msdn.microsoft.com/en-us /library/cc853327.aspx

It might not be the SaveChanges call - the first time you make any call to the database in EF, it has to do some initial code generation from the metadata. You can pre-generate this though at compile-time: http://msdn.microsoft.com/en-us/library/bb896240.aspx

I would be surprised if that's the only problem, but it might help.

Also have a look here: http://msdn.microsoft.com/en-us/library/cc853327.aspx

极度宠爱 2024-11-23 14:36:34

我会在应用程序启动时运行以下代码,看看需要多长时间,以及之后第一个 SaveChanges 是否很快。

    public static void UpdateDatabase()
    {
        //Note: Using SetInitializer is reconnended by Ladislav Mrnka with reputation 275k
        //http://stackoverflow.com/questions/9281423/entity-framework-4-3-run-migrations-at-application-start
        Database.SetInitializer<DAL.MyDbContext>(
            new MigrateDatabaseToLatestVersion<DAL.MyDbContext,
            Migrations.MyDbContext.Configuration>());

        using (var db = new DAL.MyDbContext()) { 
            db.Database.Initialize(false);//Execute the migrations now, not at the first access
        }
    }

I would run the following code on app start up and see how long it takes and if after that the first SaveChanges is fast.

    public static void UpdateDatabase()
    {
        //Note: Using SetInitializer is reconnended by Ladislav Mrnka with reputation 275k
        //http://stackoverflow.com/questions/9281423/entity-framework-4-3-run-migrations-at-application-start
        Database.SetInitializer<DAL.MyDbContext>(
            new MigrateDatabaseToLatestVersion<DAL.MyDbContext,
            Migrations.MyDbContext.Configuration>());

        using (var db = new DAL.MyDbContext()) { 
            db.Database.Initialize(false);//Execute the migrations now, not at the first access
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文