Ejemplo n.º 1
0
 public EntityModel.Contact Add(EntityModel.Contact contact)
 {
     contact.CreatedDate = DateTime.Now;
     contact.UpdatedDate = DateTime.Now;
     _context.Add(contact);
     return(_context.SaveChanges() == 1 ? contact : null);
 }
Ejemplo n.º 2
0
        public void OnGet()
        {
            var context1            = new ContactsDbContext(new DbContextOptionsBuilder <ContactsDbContext>().UseNpgsql("Server=localhost;Database=aspnet-Postgres-53bc9b9d-9d6a-45d4-8429-2a2761773502;UserId=yourUserName;Password=yourPassword").Options);
            var contactFromContext1 = context1.Contacts.FirstOrDefault(c => c.Name == "Test");

            if (contactFromContext1 == null)
            {
                contactFromContext1 = new Contact
                {
                    Name = "Test"
                };

                context1.Add(contactFromContext1);
                context1.SaveChanges();
            }

            var context2            = new ContactsDbContext(new DbContextOptionsBuilder <ContactsDbContext>().UseNpgsql("Server=localhost;Database=aspnet-Postgres-53bc9b9d-9d6a-45d4-8429-2a2761773502;UserId=yourUserName;Password=yourPassword").Options);
            var contactFromContext2 = context2.Contacts.FirstOrDefault(c => c.Name == "Test");

            contactFromContext1.Address = DateTime.Now.ToString();
            contactFromContext2.Address = DateTime.UtcNow.ToString();

            context1.SaveChanges();
            context2.SaveChanges();
        }
        public void ConcurrencyTest()
        {
            var context1            = new ContactsDbContext(new DbContextOptionsBuilder <ContactsDbContext>().UseSqlite("Data Source=Database.db").Options);
            var contactFromContext1 = context1.Contacts.FirstOrDefault(c => c.Name == "Test");

            if (contactFromContext1 == null)
            {
                contactFromContext1 = new Contact
                {
                    Name = "Test"
                };

                context1.Add(contactFromContext1);
                context1.SaveChanges();
            }

            var context2            = new ContactsDbContext(new DbContextOptionsBuilder <ContactsDbContext>().UseSqlite("Data Source=Database.db").Options);
            var contactFromContext2 = context2.Contacts.FirstOrDefault(c => c.Name == "Test");

            contactFromContext1.Address = DateTime.Now.ToString();
            contactFromContext2.Address = DateTime.UtcNow.ToString();

            context1.SaveChanges();
            context2.SaveChanges();
        }
        public void OnGet()
        {
            var context1            = new ContactsDbContext(new DbContextOptionsBuilder <ContactsDbContext>().UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=aspnet-SqlServer-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true").Options);
            var contactFromContext1 = context1.Contacts.FirstOrDefault(c => c.Name == "Test");

            if (contactFromContext1 == null)
            {
                contactFromContext1 = new Contact
                {
                    Name = "Test"
                };

                context1.Add(contactFromContext1);
                context1.SaveChanges();
            }

            var context2            = new ContactsDbContext(new DbContextOptionsBuilder <ContactsDbContext>().UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=aspnet-SqlServer-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true").Options);
            var contactFromContext2 = context2.Contacts.FirstOrDefault(c => c.Name == "Test");

            contactFromContext1.Address = DateTime.Now.ToString();
            contactFromContext2.Address = DateTime.UtcNow.ToString();

            context1.SaveChanges();
            context2.SaveChanges();
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create(Contact contact)
        {
            _context.Add(contact);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,Lastname")] Contact contact)
        {
            if (ModelState.IsValid)
            {
                _context.Add(contact);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(contact));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Create([Bind("Id,Name,Address,City,State,PostalCode,Phone,Email")] Contact contact)
        {
            if (ModelState.IsValid)
            {
                _context.Add(contact);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(contact));
        }
 public void Create(Contact contact)
 {
     _context.Add(contact);
     _context.SaveChanges();
 }