شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Ef

Public general use code classes and xml files that we've compiled and used over the years:

Entity Framework support function

   1:  using System;
   2:  using System.Web;
   3:  using System.Xml;
   4:  using System.IO;
   5:  using System.Text.RegularExpressions;
   6:  using System.Collections;
   7:  using System.Collections.Generic;
   8:  using System.Linq;
   9:  using System.Reflection;
  10:  using System.Data;
  11:  //using System.Data.Linq.Mapping;
  12:  //using System.Data.Objects;
  13:  using System.Data.Entity.Core;
  14:  using System.Data.Entity.Core.Mapping;
  15:  using System.Data.Entity.Core.Objects.DataClasses;
  16:  using System.Data.Entity.Infrastructure;
  17:  using System.Data.SqlClient;
  18:  using System.Data.Entity.Edm;
  19:   
  20:  namespace Ia.Cl.Model
  21:  {
  22:      ////////////////////////////////////////////////////////////////////////////
  23:   
  24:      /// <summary>
  25:      ///
  26:      /// </summary>
  27:      public class Ef
  28:      {
  29:          ////////////////////////////////////////////////////////////////////////////
  30:   
  31:          /// <summary publish="true">
  32:          /// Entity Framework support function
  33:          /// </summary>
  34:          /// <value>
  35:          /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
  36:          /// </value>
  37:          /// <remarks> 
  38:          /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  39:          ///
  40:          /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  41:          /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  42:          ///
  43:          /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  44:          /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  45:          /// 
  46:          /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  47:          /// 
  48:          /// Copyright notice: This notice may not be removed or altered from any source distribution.
  49:          /// </remarks> 
  50:          public Ef() { }
  51:   
  52:          /* TO DO:
  53:           * EF XML BACKUP/RESTORE OF DATABASE (setup procedure to backup and restore tables?)
  54:           */
  55:   
  56:          /// <summary/>
  57:          public enum F
  58:          {
  59:              /// <summary/>
  60:              Unknown,
  61:   
  62:              /// <summary/>
  63:              Not_Found_Now_Created,
  64:   
  65:              /// <summary/>
  66:              Found_Pending_Ready,
  67:   
  68:              /// <summary/>
  69:              Found_Pending_Past_Due,
  70:   
  71:              /// <summary/>
  72:              Found_Ready
  73:          };
  74:   
  75:          /*
  76:          ////////////////////////////////////////////////////////////////////////////
  77:  
  78:          /// <summary>
  79:          ///
  80:          /// </summary>
  81:          public static void BulkInsert<T>(string connection, string tableName, IList<T> list)
  82:          {
  83:              using (var bulkCopy = new SqlBulkCopy(connection))
  84:              {
  85:                  Type t = typeof(T);
  86:                  bulkCopy.BatchSize = list.Count;
  87:                  bulkCopy.DestinationTableName = tableName;
  88:  
  89:                  var table = new DataTable();
  90:                  var props = t.GetProperties().Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System")).ToArray();
  91:  
  92:                  foreach (var propertyInfo in props)
  93:                  {
  94:                      bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
  95:                      table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
  96:                  }
  97:  
  98:                  var values = new object[props.Length];
  99:                  foreach (var item in list)
 100:                  {
 101:                      for (var i = 0; i < values.Length; i++)
 102:                      {
 103:                          values[i] = props[i].GetValue(item);
 104:                      }
 105:  
 106:                      table.Rows.Add(values);
 107:                  }
 108:  
 109:                  bulkCopy.WriteToServer(table);
 110:              }
 111:  
 112:              /*
 113:              using (SqlConnection dbConnection = new SqlConnection("Data Source=ProductHost;Initial Catalog=yourDB;Integrated Security=SSPI;"))
 114:              {
 115:                  dbConnection.Open();
 116:                  using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
 117:                  {
 118:                      s.DestinationTableName = "Your table name";
 119:                      foreach (var column in csvFileData.Columns)
 120:                          s.ColumnMappings.Add(column.ToString(), column.ToString());
 121:                      s.WriteToServer(csvFileData);
 122:                  }
 123:              } 
 124:               * /
 125:          }
 126:           */ 
 127:   
 128:          ////////////////////////////////////////////////////////////////////////////
 129:          ////////////////////////////////////////////////////////////////////////////
 130:   
 131:          /// <summary>
 132:          ///
 133:          /// </summary>
 134:   
 135:          public struct EfTableUpdateType
 136:          {
 137:              public const int DoNothing = 1;
 138:              public const int InsertNewRecordsOnly = 2;
 139:              public const int UpdateRecords = 3;
 140:          }
 141:   
 142:          ////////////////////////////////////////////////////////////////////////////
 143:   
 144:          /// <summary>
 145:          ///
 146:          /// </summary>
 147:          public static bool UpdateTable<T>(System.Data.Entity.DbSet<T> t, ref List<T> tList, int efTableUpdateType) where T : class
 148:          {
 149:              // below:
 150:              // - Key must be defined for the T entity
 151:              // - Any list must have unique key values
 152:   
 153:              bool b;
 154:              ArrayList keyArrayList;
 155:              List<T> list;
 156:   
 157:              b = true;
 158:   
 159:              PropertyInfo propInfo;
 160:              object itemValue;
 161:   
 162:              if (efTableUpdateType == EfTableUpdateType.DoNothing)
 163:              {
 164:              }
 165:              else if (efTableUpdateType == EfTableUpdateType.InsertNewRecordsOnly)
 166:              {
 167:                  // below: we will read all keys into ArrayList
 168:   
 169:                  keyArrayList = new ArrayList(tList.Count());
 170:   
 171:                  list = (from q in t select q).ToList();
 172:   
 173:                  foreach (T se in list)
 174:                  {
 175:                      propInfo = se.GetType().GetProperty("IMPU");
 176:                      itemValue = propInfo.GetValue(se, null);
 177:   
 178:                      keyArrayList.Add(itemValue);
 179:                  }
 180:   
 181:                  foreach (T se in tList)
 182:                  {
 183:                      propInfo = se.GetType().GetProperty("IMPU");
 184:                      itemValue = propInfo.GetValue(se, null);
 185:   
 186:                      if (!keyArrayList.Contains(itemValue)) t.Add(se);
 187:                  }
 188:              }
 189:              else if (efTableUpdateType == EfTableUpdateType.UpdateRecords)
 190:              {
 191:   
 192:              }
 193:   
 194:              return b;
 195:          }
 196:   
 197:          ////////////////////////////////////////////////////////////////////////////
 198:          ////////////////////////////////////////////////////////////////////////////
 199:      }
 200:  }