Exemplo n.º 1
0
        static void Main(string[] args)
        {
            DbContextOptionsBuilder <ActorContext> optionsBuilder = new DbContextOptionsBuilder <ActorContext>();

            optionsBuilder.UseSqlServer(@"Server=actors-db;Database=Actors;User=sa;Password=Your_password123");

            using (ActorContext context = new ActorContext(optionsBuilder.Options)) {
                context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                context.Database.Migrate();

                Parser.Default.ParseArguments <Options>(args)
                .WithParsed <Options>(o => {
                    if (o.Import)
                    {
                        //import contacts from csv
                        using (StreamReader reader = new StreamReader("/data/name.basics.tsv")) {
                            Configuration config = new Configuration();
                            config.Delimiter     = "\t";

                            int counter = 0;
                            using (CsvReader csv = new CsvReader(reader, config)) {
                                foreach (ActorRecord record in csv.EnumerateRecords(new ActorRecord()))
                                {
                                    Person person = new Person();

                                    //parse csv data
                                    int birthYear = 0;
                                    Int32.TryParse(record.BirthYear, out birthYear);
                                    int deathYear = -1;
                                    Int32.TryParse(record.DeathYear, out deathYear);

                                    if (!context.Persons.Any(x => x.Nconst == record.Nconst))
                                    {
                                        //map csv to models.
                                        person.Nconst    = record.Nconst;
                                        person.Name      = record.PrimaryName;
                                        person.BirthYear = birthYear;
                                        person.DeathYear = deathYear == -1 ? null: (int?)deathYear;

                                        context.Add(person);

                                        //only save after 1000 entries to speed things up a bit.
                                        if (counter % 1000 == 0)
                                        {
                                            context.SaveChanges();
                                            counter = 0;
                                        }
                                        counter += 1;
                                    }
                                }
                            }
                        }

                        //save anything that hasn't been saved yet.
                        context.SaveChanges();
                    }
                });
            }
        }