خرید بک لینک

Vote count: 0

Let's say I'm reading data from an excel file and I need to save this using EF. After reading excel, I construct an entity of type in my EF model - say Holiday.

Chance is that this particular holiday already exist in my DB or may not. I can't know it just by reading the excel file. So, I check whether that entity exist, if so I have to modify the existing entity or add a new entity.

Following is how I'm handling the logic.

 foreach (var holiday in clientDataObj.Holidays)
        {
            // to check for existing holidays, no need to consider the location, as all holidays will anyway exist at root level.
            Holiday existingHoliday =
                ClientContext.Holidays.SingleOrDefault(h => string.Equals(h.Date, holiday.Date));
            if (existingHoliday == null)
            {
                rootLocation.Holidays.Add(holiday);
            }
            else
            {
                holiday.HolidayId = existingHoliday.HolidayId;
                ClientContext.Entry(existingHoliday).State = System.Data.Entity.EntityState.Detached;
                ClientContext.Entry(holiday).State = System.Data.Entity.EntityState.Modified;
            }
        }

        ClientContext.SaveChanges();

I haven't yet run the code to see if it's working. Can someone give an idea of whether this is the way to do it, or is there any other best practice in addressing this problem.

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 17 ارديبهشت 1395 ساعت: 11:22

صفحه بندی