实体框架未将更改保存到数据库中 [英] Entity Framework not Saving Changes into Database

查看:71
本文介绍了实体框架未将更改保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对为什么此代码不起作用感到困惑,它应该在循环后将更改保存到数据库中,但是当我将SaveChanges方法放在循环内时,它将记录保存到数据库中,但是在记录外却没有不保存任何东西?大约只有300〜1000条记录

I'm puzzled as to why this code is not working, it should save changes to database after the loops but when I place the SaveChanges method inside the loop, it saves the record into the database but outside it doesn't save anything? it's about only 300 ~ 1000 records

    static bool lisReady = false;
    static bool sacclReady = false;

    static void Main(string[] args)
    {
        Logger("Starting services");
        ConnectDBLis().Wait();
        ConnectDBSaccl().Wait();
        Thread.Sleep(1000);
        if (lisReady & sacclReady){
            //start
            Logger("Services ready");
            StartExport().Wait();
        }
    }

    static async Task<bool> StartExport()
        {
            lis lisdb = new lis();
            nrlsaccl saccldb = new nrlsaccl();
            var getTestOrders = await lisdb.test_orders.ToListAsync();
            Logger("Services starting");
            foreach (var tO in getTestOrders.Where(x => x.entry_datetime.Value.Year == 2016))
            {
                foreach (var tr in tO.test_results)
                {
                    foreach (var tL in tr.test_result_logs)
                    {
                        results_availability postResults = new results_availability
                        { 
                          first_name = tO.patient_orders.patient.first_name,
                          middle_name = tO.patient_orders.patient.middle_name,
                          last_name = tO.patient_orders.patient.last_name,
                          birthdate = tO.patient_orders.patient.birthdate,
                        };
                        if (postResults.id == 0)
                        {
                            saccldb.results_availability.Add(postResults);
                        }
                        else
                        {
                            saccldb.Entry(postResults).State = EntityState.Modified;
                        }
                    }
                }
            }
            await saccldb.SaveChangesAsync();
            return true;
        }

所以我将记录限制为100条,并且保存更改有效,即时3000条记录不起作用,有什么解决方案吗?

So i limit the records to 100 and the save changes works, 3000 records at instant does not work, any solutions?

推荐答案

使用

saccldb.SaveChanges()

仅因为await saccldb.SaveChangesAsync()的异步特性导致线程继续执行并退出函数,然后完成保存到数据库的操作.就您而言,它返回true.

Simply because the async nature of await saccldb.SaveChangesAsync() cause your thread to continue and exit the function before the saving to the db completes. In your case it returns true.

我建议不要在控制台应用程序上使用任何异步操作,除非它具有您希望继续使用的用户界面.

I would suggest not using any async operations on a console application unless it has a user interface that you would want to keep going.

这篇关于实体框架未将更改保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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