SaveChanges() public method

public SaveChanges ( ) : int
return int
Exemplo n.º 1
7
        public void TestEmployeeAdd()
        {
            var context = new AppDbContext();

            var emp = new Employee
            {
                Id=Guid.NewGuid(),
                Code = "001",
                FirstName = "ali",
                LastName = "hassanzade"
            };

            context.Set<Employee>().Add(emp);

            context.SaveChanges();
        }
Exemplo n.º 2
0
        public void SandboxTest()
        {
            var context = new AppDbContext();

            var c1 = new Choice { Id = 1 };
            var c3 = new Choice { Id = 3 };

            var freeAnswer = new FreeTextAnswer { Comment = "my comments", QuestionId = 2 };

            var multipleAns = new MultipleChoicesAnswer
            {
                QuestionId = 1,
                SelectedChoices = new List<Choice> { c1, c3 }
            };

            var answer = new PollAnswer
            {
                AnswerDate = DateTime.Now,
                PollId = 1,
                QuestionAnswers = new List<QuestionAnswer> { freeAnswer, multipleAns }
            };

            context.PollAnswers.AddOrUpdate(answer);
            context.SaveChanges();
        }
Exemplo n.º 3
0
        // Moved to teste stack trace file location

        internal static void Delete()
        {
            using (AppDbContext dbContext = new AppDbContext())
            {
                dbContext.Products.RemoveRange(dbContext.Products.Take(10).AsEnumerable());
                dbContext.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public void TestRemoveEmployeeByIRemovable()
        {
            var context = new AppDbContext();

            var emp = context.Set<Employee>().First();

            emp.Remove();

            context.SaveChanges();
        }
Exemplo n.º 5
0
 public void UpdateBalance(int accountId, decimal newBalance)
 {
     var account = new Account
     {
         Id = accountId,
         CurrentBalance = newBalance
     };
     var context = new AppDbContext();
     context.Accounts.Attach(account);
     context.Entry(account).Property(u => u.CurrentBalance).IsModified = true;
     context.SaveChanges();
 }
Exemplo n.º 6
0
        private static void Main() {
            Console.WriteLine("Initializing...");
            
            Database.SetInitializer(new AppDbContext.Initializer());

            using (AppDbContext dbContext = new AppDbContext()) {
                // Ef is lazy and really only initializes on first query
                if (!dbContext.Products.Any()) dbContext.SaveChanges();
            }

            Console.WriteLine("Initialized");
            Console.WriteLine();

            while (true) {
                Console.Write("[S]elect / Select [N]+1 / [C]ount / [A]dd / [D]elete: _\b");

                var k = Char.ToLower(Console.ReadKey().KeyChar);
                Console.WriteLine();

                switch (k) {
                    case 's':
                        Repository1.Select();
                        break;

                    case 'n':
                        Repository1.SelectN1();
                        break;

                    case 'c':
                        Repository1.SelectCount();
                        break;

                    case 'a':
                        Repository2.Add();
                        break;

                    case 'd':
                        Repository2.Delete();
                        break;
                }

                if (k == 'q') {
                    break;
                }

                Console.WriteLine();
            }
        }
Exemplo n.º 7
0
        //[HttpPost]
        public ActionResult Create(string login, string password)
        {
            User user = null;
            using (var context = new AppDbContext())
            {
                user = new User();
                user.Login = login;

                var hashManager = new PBKDF2HashManager();
                var keySaltPair = hashManager.GetKeyAndSalt(password);

                user.PasswordKey = keySaltPair.Key;
                user.PasswordSalt = keySaltPair.Salt;

                context.Users.Add(user);
                context.SaveChanges();
            }

            return View(user);
        }
Exemplo n.º 8
0
        public void TestMethod1()
        {
            using (AppDbContext appDb = new AppDbContext()) {
                appDb.Items.Add(new AppItems() {
                    Name = "Item1",
                });
                appDb.Items.Add(new AppItems()
                {
                    Name = "Item2",
                });
                appDb.Items.Add(new AppItems()
                {
                    Name = "Item3",
                });
                appDb.SaveChanges();
            }
            using (FooDbContext fooDb = new FooDbContext()) {
                fooDb.FooItems.Add(new FooItems() {
                    Name = "Foo1",
                });
                fooDb.FooItems.Add(new FooItems()
                {
                    Name = "Foo2",
                });
                fooDb.FooItems.Add(new FooItems()
                {
                    Name = "Foo3",
                });
                fooDb.SaveChanges();
            }

            // Query saved items
            using (AppDbContext appDb = new AppDbContext()) {
                var items = appDb.Items.ToList();
                items.ForEach(c => log.Info("AppItem:" + c.Guid.ToString() + " name:" + c.Name));
            }
            using (FooDbContext fooDb = new FooDbContext()) {
                var items = fooDb.FooItems.ToList();
                items.ForEach(c => log.Info("FooItem: " + c.Guid.ToString() + " name:" + c.Name));
            }
        }
Exemplo n.º 9
0
 public void Configuration(IAppBuilder app)
 {
     ConfigureAuth(app);
     Database.SetInitializer(new DropCreateDatabaseAlways<AppDbContext>());
     using (var db = new AppDbContext())
     {
         db.Companies.Add(new Company
         {
             Title = "Company1",
             Size = "Small",
             Customers = new List<Customer>
             {
                 new Customer()
                 {
                     Name = "Company1.Customer1",
                     Age = 42,
                     Cars = new List<Car>()
                     {
                         new Car()
                         {
                             Make = "Audi",
                             Year = 2013
                         },
                         new Car()
                         {
                             Make = "BMW",
                             Year = 2003
                         }
                     }
                 },
                 new Customer()
                 {
                     Name = "Company1.Customer2",
                     Age = 15
                 },
             }
         });
         db.SaveChanges();
     }
 }
		public static void Init()
		{
			using (var context = new AppDbContext())
			{
				if (!context.Users.Any())
				{
					var manager = new ApplicationUserManager(new UserStore<User>(context));
					manager.Create(new User
					{
						Email = "*****@*****.**",
						UserName = "******",
					}, "Password1");

					manager.Create(new User
					{
						Email = "*****@*****.**",
						UserName = "******",
					}, "Password1");

					manager.Create(new User
					{
						Email = "*****@*****.**",
						UserName = "******",
					}, "Password1");
				}

				if (!context.Customers.Any())
				{
					AddNewCustomers(context);

					AddExistingCustomers(context);

					AddTerminatedCustomers(context);

					context.SaveChanges();
				}
			}
		}
Exemplo n.º 11
0
		public static void Init()
		{
			using (var context = new AppDbContext())
			{
				if (!context.Users.Any())
				{
					var manager = new ApplicationUserManager(new UserStore<User>(context));
					manager.Create(new User
					{
						Email = "*****@*****.**",
						UserName = "******",
					}, "Password1");

					manager.Create(new User
					{
						Email = "*****@*****.**",
						UserName = "******",
					}, "Password1");

					manager.Create(new User
					{
						Email = "*****@*****.**",
						UserName = "******",
					}, "Password1");
				}

				if (!context.Customers.Any())
				{
					AddNewCustomers(context);

					AddExistingCustomers(context);

					AddTerminatedCustomers(context);

					context.SaveChanges();
				}
			}
		}
        private static void AddCustomData()
        {
            using (var ctx = new AppDbContext())
            {
                if (!ctx.Empresas.Any())
                {
                    ctx.Empresas.Add(new Empresa
                    {
                        Id = 1,
                        Nome = "Empresa 1"
                    });

                    ctx.Empresas.Add(new Empresa
                    {
                        Id = 2,
                        Nome = "Empresa 2"
                    });

                    ctx.Empresas.Add(new Empresa
                    {
                        Id = 3,
                        Nome = "Empresa 3"
                    });

                    ctx.SaveChanges();
                }

                if (!ctx.Filiais.Any())
                {
                    ctx.Filiais.Add(new Filial
                    {
                        EmpresaId = 1,
                        Id = 11,
                        Nome = "Empresa 1 / Filial 1"
                    });

                    ctx.Filiais.Add(new Filial
                    {
                        EmpresaId = 1,
                        Id = 12,
                        Nome = "Empresa 1 / Filial 2"
                    });

                    ctx.Filiais.Add(new Filial
                    {
                        EmpresaId = 2,
                        Id = 21,
                        Nome = "Empresa 2 / Filial 1"
                    });

                    ctx.Filiais.Add(new Filial
                    {
                        EmpresaId = 2,
                        Id = 22,
                        Nome = "Empresa 2 / Filial 2"
                    });

                    ctx.Filiais.Add(new Filial
                    {
                        EmpresaId = 3,
                        Id = 31,
                        Nome = "Empresa 3 / Filial 1"
                    });

                    ctx.Filiais.Add(new Filial
                    {
                        EmpresaId = 3,
                        Id = 32,
                        Nome = "Empresa 3 / Filial 2"
                    });

                    ctx.SaveChanges();
                }

                using (var userManager = new AppUserManager(new UserStore<Usuario>(ctx)))
                {
                    if (!ctx.Users.Any())
                    {
                        userManager.Create(new Usuario
                        {
                            EmpresaId = 1,
                            UserName = "******",
                            Email = "*****@*****.**",
                            Perfil = Perfil.Empresa
                        }, "123456");
                    }
                }
            }
        }
Exemplo n.º 13
-1
        private void CreatePoll(AppDbContext context)
        {
            var multiple = new MultipleChoicesQuestion
            {
                Type = QuestionType.MultipleChoices,
                Statement = "multiple choices",
                CanSelectMultiple = true,
                Choices =
                                       new List<Choice>
                                           {
                                               new Choice { Text = "11111" },
                                               new Choice { Text = "22222" },
                                               new Choice { Text = "33333" }
                                           }
            };

            var freeText = new Question { Statement = "free text", Type = QuestionType.FreeText };

            var poll = new Poll
            {
                Name = "agora vai",
                Range = 50,
                CreationDate = DateTime.Now,
                ExpirationDate = new DateTime(2020, 01, 01),
                CreationLocation = new Location { Latitude = 90.0f, Longitude = 50.0f },
                Questions = new List<Question> { multiple, freeText },
            };

            context.Polls.Add(poll);
            context.SaveChanges();
        }