bulk insert and update with ADO.NET Entity Framework
        Posted  
        
            by Keith Barrows
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Keith Barrows
        
        
        
        Published on 2010-04-06T19:09:22Z
        Indexed on 
            2010/04/06
            19:13 UTC
        
        
        Read the original article
        Hit count: 2050
        
I am writing a small application that does a lot of feed processing. I want to use LINQ EF for this as speed is not an issue, it is a single user app and, in the end, will only be used once a month.
My questions revolves around the best way to do bulk inserts using LINQ EF.
After parsing the incoming data stream I end up with a List of values. Since the end user may end up trying to import some duplicate data I would like to "clean" the data during insert rather than reading all the records, doing a for loop, rejecting records, then finally importing the remainder.
This is what I am currently doing:
DateTime minDate = dataTransferObject.Min(c => c.DoorOpen);
DateTime maxDate = dataTransferObject.Max(c => c.DoorOpen);
using (LabUseEntities myEntities = new LabUseEntities())
{
    var recCheck = myEntities.ImportDoorAccess.Where(a => a.DoorOpen >= minDate && a.DoorOpen <= maxDate).ToList();
    if (recCheck.Count > 0)
    {
        foreach (ImportDoorAccess ida in recCheck)
        {
            DoorAudit da = dataTransferObject.Where(a => a.DoorOpen == ida.DoorOpen && a.CardNumber == ida.CardNumber).First();
            if (da != null)
                da.DoInsert = false;
        }
    }
    ImportDoorAccess newIDA;
    foreach (DoorAudit newDoorAudit in dataTransferObject)
    {
        if (newDoorAudit.DoInsert)
        {
            newIDA = new ImportDoorAccess
            {
                CardNumber = newDoorAudit.CardNumber,
                Door = newDoorAudit.Door,
                DoorOpen = newDoorAudit.DoorOpen,
                Imported = newDoorAudit.Imported,
                RawData = newDoorAudit.RawData,
                UserName = newDoorAudit.UserName
            };
            myEntities.AddToImportDoorAccess(newIDA);
        }
    }
    myEntities.SaveChanges();
}
I am also getting this error:
System.Data.UpdateException was unhandled
Message="Unable to update the EntitySet 'ImportDoorAccess' because it has a DefiningQuery and no element exists in the element to support the current operation."
Source="System.Data.SqlServerCe.Entity"
What am I doing wrong?
Any pointers are welcome.
© Stack Overflow or respective owner