EF 구조 ~ 성능 효율 적 인 대량 작업 (Insert 편)

11455 단어 insert
디렉토리 로 돌아 가기
linq to sql 이 든 entity frameworks 든 목록 작업 을 할 때 한 가지 문제 가 있 습 니 다. 그것 은 하나의 실체 만 서버 에 보 낼 수 있다 는 것 입 니 다. 그러면 목록 의 수량 이 많 습 니 다. 예 를 들 어 목록 이 10 만 개의 데이터 라면 이런 조작 은 매우 성능 이 있 을 것 입 니 다. 아마도 DB 가 끊 을 것 입 니 다.
솔 루 션: T - SQL 문자열 을 연결 하여 유 니 버 설 로 만 듭 니 다.
장점: 서버 와 한 번 연결 하여 서버 에 SQL 명령 을 보 내 면 가능 합 니 다.
코드 는 다음 과 같 습 니 다:
 1   /// <summary>
 2         ///   Insert   
 3         ///       ,      0,        SQL  
 4         /// </summary>
 5         /// <typeparam name="TEntity"></typeparam>
 6         /// <param name="entity"></param>
 7         /// <returns></returns>
 8         private Tuple<string, object[]> CreateInsertSQL<TEntity>(TEntity entity) where TEntity : class
 9         {
10             if (entity == null)
11                 throw new ArgumentException("The database entity can not be null.");
12 
13             Type entityType = entity.GetType();
14             var table = entityType.GetProperties().Where(i => i.PropertyType != typeof(EntityKey)
15                && i.PropertyType != typeof(EntityState)
16                && i.GetValue(entity, null) != null
17                && (i.PropertyType.IsValueType || i.PropertyType == typeof(string)))
18                .ToArray();//    ,    ,     
19             List<string> pkList = GetPrimaryKey<TEntity>().Select(i => i.Name).ToList();
20 
21             List<object> arguments = new List<object>();
22             StringBuilder fieldbuilder = new StringBuilder();
23             StringBuilder valuebuilder = new StringBuilder();
24 
25             fieldbuilder.Append(" INSERT INTO " + string.Format("[{0}]", entityType.Name) + " (");
26 
27             foreach (var member in table)
28             {
29                 if (pkList.Contains(member.Name) && Convert.ToString(member.GetValue(entity, null)) == "0")
30                     continue;
31                 object value = member.GetValue(entity, null);
32                 if (value != null)
33                 {
34                     if (arguments.Count != 0)
35                     {
36                         fieldbuilder.Append(", ");
37                         valuebuilder.Append(", ");
38                     }
39 
40                     fieldbuilder.Append(member.Name);
41                     if (member.PropertyType == typeof(string) || member.PropertyType == typeof(DateTime))
42                         valuebuilder.Append("'{" + arguments.Count + "}'");
43                     else
44                         valuebuilder.Append("{" + arguments.Count + "}");
45                     if (value.GetType() == typeof(string))
46                         value = value.ToString().Replace("'", "char(39)");
47                     arguments.Add(value);
48 
49                 }
50             }
51 
52 
53             fieldbuilder.Append(") Values (");
54 
55             fieldbuilder.Append(valuebuilder.ToString());
56             fieldbuilder.Append(");");
57             return new Tuple<string, object[]>(fieldbuilder.ToString(), arguments.ToArray());
58         }

그 후에 저 는 업데이트 작업 과 삭제 작업 을 계속 하고 삭제 작업 을 봉인 하여 여러분 께 바 치 겠 습 니 다. 기대 하 시기 바 랍 니 다.
디렉토리 로 돌아 가기

좋은 웹페이지 즐겨찾기