本文介绍了大容量插入和更新与ADO.NET实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个小的应用程序,做了很多饲料加工。我想使用LINQ EF基于这一点,速度不是一个问题,它是一个单用户的应用程序,并在结束时,将只用于每月一次。

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.

我的问题是围绕使用LINQ EF做批量插入的最佳方法。

My questions revolves around the best way to do bulk inserts using LINQ EF.

解析传入数据流我最终值的列表之后。由于最终用户最终可能会尝试导入一些重复的数据,我想清理插入过程中的数据,而不是阅读所有的记录,做一个for循环,拒绝记录,最后导入的剩余部分。

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.

这是我目前做的:

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是未处理
   消息=无法更新EntitySet的'ImportDoorAccess,因为它有一个DefiningQuery和支持当前操作元素没有元素存在。
   来源=System.Data.SqlServerCe.Entity

我是什么做错了吗?

任何指针是欢迎的。

推荐答案

您可以做多个插入这样的。

You can do multiple inserts this way.

我见过你要在模型(EDMX)设置不正确的情况下例外。你要么没有该表的主键(在的EntityKey EF而言),或设计师一直试图猜出的EntityKey应该的。在后一种情况下,你会看到EDM设计与按键两种或两种以上性质的旁边。

I've seen the exception you're getting in cases where the model (EDMX) is not set up correctly. You either don't have a primary key (EntityKey in EF terms) on that table, or the designer has tried to guess what the EntityKey should be. In the latter case, you'll see two or more properties in the EDM Designer with keys next to them.

请确保该 ImportDoorAccess 表中有一个主键,刷新模式。

Make sure the ImportDoorAccess table has a single primary key and refresh the model.

这篇关于大容量插入和更新与ADO.NET实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:55