내가 두 테이블이 관련 항목 (다 대다 관계)와 엔티티를 저장이 방법foreach 루프에서 엔티티를 추가하거나 업데이트하면 SaveChanges()를 호출하기 전에 시간이 너무 오래 걸립니까?
private static void Save<T>(TbCommonHistoryLog log, List<T> lstDetails) where T : IHasSerial
{
foreach (var item in lstDetails.OrderBy(x => x.Serial))
{
var ser = SerializeObject(item);
var record = oContext.TbHistoryLog_Lists.FirstOrDefault(x => x.ListObjectJson == ser);
if (record == null) //add new list item
{
TbCommonHistoryLog_Lists listObject = new TbCommonHistoryLog_Lists()
{
ListObjectJson = SerializeObject(item)
};
var details = new TbCommonHistoryLogDetails { TbHistoryLog = log, TbHistoryLog_Lists = listObject };
oContext.TbHistoryLogDetails.Add(details);
}
else //attach an existing list item
{
var o = oContext.TbHistoryLog_Lists.Find(record.Id);
oContext.TbHistoryLog_Lists.Attach(o);
var details = new TbCommonHistoryLogDetails { TbHistoryLog = log, TbHistoryLog_Lists = o };
oContext.TbHistoryLogDetails.Add(details);
}
}
oContext.BulkSaveChanges();
}
있습니다 TbCommonHistoryLog
, TbCommonHistoryLog_Lists
많은 관계로 많은에있는, 조인 테이블 TbCommonHistoryLogDetails
, 여기서 내가하는 일은 master-detail 모델에 대한 감사이고 모든 감사는 DB의 JSON에 직렬화되며 머리 객체는 TbCommonHistoryLog
테이블에 저장하고 TbHistoryLog_Lists
테이블의 모든 목록 항목은 mthod에 저장합니다. 위의 목록 항목이 데이터베이스에 이미 존재하는지 확인하거나 복제를 방지합니다. 하지만이 프로세스는 매우 오랜 시간이 걸리는 15 초 이상 걸립니다. 여기서 내가 잘못하고있는 것이 무엇인지 알 수 없습니다. 제발 도와주세요.