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

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 Microsoft.EntityFrameworkCore.Core;
  14:  //using Microsoft.EntityFrameworkCore.Core.Mapping;
  15:  //using Microsoft.EntityFrameworkCore.Core.Objects.DataClasses;
  16:  using Microsoft.EntityFrameworkCore.Infrastructure;
  17:  using System.Data.SqlClient;
  18:  //using Microsoft.EntityFrameworkCore.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), Integrated 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:  
 145:          /// <summary>
 146:          ///
 147:          /// </summary>
 148:          public static bool UpdateTable<T>(System.Data.Entity.DbSet<T> t, ref List<T> tList, int efTableUpdateType) where T : class
 149:          {
 150:              // below:
 151:              // - Key must be defined for the T entity
 152:              // - Any list must have unique key values
 153:  
 154:              bool b;
 155:              ArrayList keyArrayList;
 156:              List<T> list;
 157:  
 158:              b = true;
 159:  
 160:              PropertyInfo propInfo;
 161:              object itemValue;
 162:  
 163:              if (efTableUpdateType == EfTableUpdateType.DoNothing)
 164:              {
 165:              }
 166:              else if (efTableUpdateType == EfTableUpdateType.InsertNewRecordsOnly)
 167:              {
 168:                  // below: we will read all keys into ArrayList
 169:  
 170:                  keyArrayList = new ArrayList(tList.Count());
 171:  
 172:                  list = (from q in t select q).ToList();
 173:  
 174:                  foreach (T se in list)
 175:                  {
 176:                      propInfo = se.GetType().GetProperty("IMPU");
 177:                      itemValue = propInfo.GetValue(se, null);
 178:  
 179:                      keyArrayList.Add(itemValue);
 180:                  }
 181:  
 182:                  foreach (T se in tList)
 183:                  {
 184:                      propInfo = se.GetType().GetProperty("IMPU");
 185:                      itemValue = propInfo.GetValue(se, null);
 186:  
 187:                      if (!keyArrayList.Contains(itemValue)) t.Add(se);
 188:                  }
 189:              }
 190:              else if (efTableUpdateType == EfTableUpdateType.UpdateRecords)
 191:              {
 192:  
 193:              }
 194:  
 195:              return b;
 196:          }
 197:          */
 198:   
 199:          ////////////////////////////////////////////////////////////////////////////
 200:          ////////////////////////////////////////////////////////////////////////////
 201:      }
 202:  }