Entity Framework not Saving Changes into Database

c# entity-framework entity-framework-6 mysql

Question

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;
        }

Edit:

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

1
4
4/19/2016 8:22:43 AM

Popular Answer

Use

saccldb.SaveChanges()

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.

0
4/19/2016 8:39:21 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow