Last active
August 29, 2015 14:07
-
-
Save AlbertoMonteiro/01c5262903cec887afcf to your computer and use it in GitHub Desktop.
Inserindo Varios DoCSV via EF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Data.Entity; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| namespace ConsoleApplication1 | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var ctx = new Contexto(); | |
| ctx.Database.CreateIfNotExists(); | |
| ctx.Configuration.AutoDetectChangesEnabled = false; | |
| ctx.Configuration.LazyLoadingEnabled = false; | |
| ctx.Configuration.ProxyCreationEnabled = false; | |
| ctx.Configuration.UseDatabaseNullSemantics = false; | |
| ctx.Configuration.ValidateOnSaveEnabled = false; | |
| var startNew = Stopwatch.StartNew(); | |
| Console.WriteLine("Lendo csv"); | |
| var clienteAgs = File.ReadLines(@"C:\Temp\VENDASTabelaGrade.csv").Skip(1) | |
| .AsParallel() | |
| .Select(l => l.Split('|')) | |
| .Select(l => new ClienteAg(l[0], l[1], l[2], l[3])) | |
| .ToList() | |
| .Partition(2000) | |
| .ToList(); | |
| startNew.Stop(); | |
| Console.WriteLine("Csv lido em: {0}", startNew.Elapsed); | |
| Console.WriteLine("Inserindo - Começando as {0:hh:mm:ss}", DateTime.Now); | |
| startNew.Restart(); | |
| InserirTudo(clienteAgs, startNew); | |
| startNew.Stop(); | |
| Console.WriteLine("Executado em: {0}", startNew.Elapsed); | |
| } | |
| public static void InserirTudo(List<List<ClienteAg>> clienteAgs, Stopwatch startNew) | |
| { | |
| var inseridos = 0; | |
| Parallel.ForEach(clienteAgs, items => | |
| { | |
| var ctx = new Contexto(); | |
| ctx.ClienteAgs.AddRange(items); | |
| Interlocked.Add(ref inseridos, ctx.SaveChanges()); | |
| Console.WriteLine("Inseridos {0}", inseridos); | |
| }); | |
| } | |
| } | |
| public static class MyClass | |
| { | |
| public static IEnumerable<List<T>> Partition<T>(this IList<T> source, Int32 size) | |
| { | |
| for (int i = 0; i < Math.Ceiling(source.Count / (Double)size); i++) | |
| yield return new List<T>(source.Skip(size * i).Take(size)); | |
| } | |
| } | |
| public class ClienteAg | |
| { | |
| public ClienteAg() | |
| { | |
| } | |
| public ClienteAg(string uid, string vendaTabelaUid, string pcpGradeUid, string valor) | |
| { | |
| Uid = uid; | |
| VendaTabelaUid = vendaTabelaUid; | |
| PcpGradeUid = pcpGradeUid; | |
| Valor = valor; | |
| } | |
| public long Id { get; set; } | |
| public string Uid { get; set; } | |
| public string VendaTabelaUid { get; set; } | |
| public string PcpGradeUid { get; set; } | |
| public string Valor { get; set; } | |
| } | |
| public class Contexto : DbContext | |
| { | |
| public DbSet<ClienteAg> ClienteAgs { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment