Ejemplo 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();
                    }
                });
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "ActorName")] Actor actor)
        {
            if (ModelState.IsValid)
            {
                db.Actors.Add(actor);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(actor));
        }
Ejemplo n.º 3
0
        public string Buy(Buying buying)
        {
            buying.DateTime = DateTime.Now;

            actorContext.Buyings.Add(buying);

            actorContext.SaveChanges();

            return($"Thanks for buying, {buying.FIO}");
        }
Ejemplo n.º 4
0
 //Save
 public void save()
 {
     _ActorContext.SaveChanges();
 }