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.º 2
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);
 }
        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();
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
        public int AddContact(Person person)
        {
            _context.Persons.Add(person);
            _context.SaveChanges();

            return(person.PersonId);
        }
Ejemplo n.º 6
0
        public void Add(Contact contact)
        {
            var contactToInsert = contact;

            contactToInsert.Id = 0;
            _context.ContactItems.Add(contact);
            _context.SaveChanges();
        }
Ejemplo n.º 7
0
        public ContactViewModel Create(ContactViewModel contact)
        {
            var contactModel = ContactToModel(contact);

            _context.Contacts.Add(contactModel);
            _context.SaveChanges();
            return(ContactToViewModel(contactModel));
        }
        private void CreateEditions()
        {
            var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);

            if (defaultEdition == null)
            {
                defaultEdition = new Edition {
                    Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName
                };
                _context.Editions.Add(defaultEdition);
                _context.SaveChanges();

                /* Add desired features to the standard edition, if wanted... */
            }
        }
        public ActionResult RegisterAdmin(CreateUserRoleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var salt = CreateSHAHash(model.Password);

            model.Password        = salt;
            model.ConfirmPassword = salt;

            if (ModelState.IsValid)
            {
                using (ContactsDbContext db = new ContactsDbContext())
                {
                    var user = db.RoleModel.FirstOrDefault(u => u.UserName == model.UserName || u.FullName == model.FullName || u.Email == model.Email);
                    if (user != null)
                    {
                        ModelState.AddModelError("", "Username or email already exists");
                    }

                    else
                    {
                        db.RoleModel.Add(model);
                        db.SaveChanges();
                        ModelState.Clear();
                    }
                }
            }

            return(View());
        }
Ejemplo n.º 10
0
 public IActionResult Post([FromBody] Users user)
 {
     try
     {
         _context._Users.Add(user);
         _context.SaveChanges();
     }
     catch
     {
         if (Exists(user.UserName))
         {
             return(Conflict());
         }
         return(BadRequest());
     }
     return(Ok());
 }
        public void Create()
        {
            new DefaultEditionCreator(_context).Create();
            new DefaultLanguagesCreator(_context).Create();
            new HostRoleAndUserCreator(_context).Create();
            new DefaultSettingsCreator(_context).Create();

            _context.SaveChanges();
        }
Ejemplo n.º 12
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new ContactsDbContext(
                       serviceProvider.GetRequiredService <DbContextOptions <ContactsDbContext> >()))
            {
                if (context.Contacts.Any())
                {
                    return;
                }

                context.Contacts.AddRange(
                    new Contact
                {
                    Id            = 1,
                    BirthDate     = new DateTime(1993, 1, 26),
                    Company       = "Springfield Nuclear Plant",
                    Email         = "*****@*****.**",
                    Name          = "Simpson Homer",
                    PhonePersonal = "827364",
                    PhoneWork     = "7895",
                    ProfileImage  = "https://lh3.googleusercontent.com/proxy/QQcqti3zIpMN2sL47ZKfV1b8_Fk2oVwGdcImqdAMMTKj862qYu1rfQPzYtuivi53sUBoupNquFl_bNYoP5oRFzsNTcwTavGVQpMcsNfTdsiVYf-FmtQ",
                    Address       = "742 Evergreen Terrace",
                    City          = "Springfield"
                },
                    new Contact
                {
                    Id            = 2,
                    BirthDate     = new DateTime(1993, 1, 26),
                    Company       = "Left Handled Market",
                    Email         = "*****@*****.**",
                    Name          = "Ned Flanders",
                    PhonePersonal = "666",
                    PhoneWork     = "999",
                    ProfileImage  = "https://vignette.wikia.nocookie.net/wikisimpsons2010/images/2/22/NED_FLANDERS_JR..png/revision/latest/scale-to-width-down/340?cb=20100929081708&path-prefix=es",
                    Address       = "743 Evergreen Terrace",
                    City          = "Springfield"
                }
                    ,
                    new Contact
                {
                    Id            = 3,
                    BirthDate     = new DateTime(1993, 1, 26),
                    Company       = "School",
                    Email         = "",
                    Name          = "Groundskeeper Willy",
                    PhonePersonal = "567890",
                    PhoneWork     = "098765",
                    ProfileImage  = "https://upload.wikimedia.org/wikipedia/en/thumb/d/dc/GroundskeeperWillie.png/220px-GroundskeeperWillie.png",
                    Address       = "741 Wood house",
                    City          = "Kirkwall"
                }
                    );

                context.SaveChanges();
            }
        }
Ejemplo n.º 13
0
        private void AddSettingIfNotExists(string name, string value, int?tenantId = null)
        {
            if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null))
            {
                return;
            }

            _context.Settings.Add(new Setting(tenantId, null, name, value));
            _context.SaveChanges();
        }
        private void AddLanguageIfNotExists(ApplicationLanguage language)
        {
            if (_context.Languages.IgnoreQueryFilters().Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                return;
            }

            _context.Languages.Add(language);
            _context.SaveChanges();
        }
        private void CreateDefaultTenant()
        {
            // Default tenant

            var defaultTenant = _context.Tenants.IgnoreQueryFilters().FirstOrDefault(t => t.TenancyName == AbpTenantBase.DefaultTenantName);

            if (defaultTenant == null)
            {
                defaultTenant = new Tenant(AbpTenantBase.DefaultTenantName, AbpTenantBase.DefaultTenantName);

                var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);
                if (defaultEdition != null)
                {
                    defaultTenant.EditionId = defaultEdition.Id;
                }

                _context.Tenants.Add(defaultTenant);
                _context.SaveChanges();
            }
        }
Ejemplo n.º 16
0
        private static void LoadTestData(ContactsDbContext context)
        {
            //TODO: load from XML
            var testContact = new Contact
            {
                Id            = 1,
                FirstName     = "Joe",
                LastName      = "Pottida",
                EmailAddress  = "*****@*****.**",
                BusinessPhone = "1-800-555-5555",
                HomePhone     = "1-800-555-5554",
                MobilePhone   = "1-800-555-5553",
                StreetAddress = "123 S. Main St.",
                City          = "Kalamazoo",
                State         = "MI",
                ZipCode       = "55555",
                Notes         = "Joe is a person."
            };

            context.Contacts.Add(testContact);
            context.SaveChanges();
        }
        private static ContactsDbContext GetDbContext()
        {
            var options = new DbContextOptionsBuilder <ContactsDbContext>()
                          .UseInMemoryDatabase("TestDB")
                          .Options;

            var context = new ContactsDbContext(options);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.Contact.AddRange(new List <Contact>
            {
                new Contact {
                    FirstName = "Sami", LastName = "Anttonen"
                },
                new Contact {
                    FirstName = "Joku", LastName = "Toinen"
                }
            });
            context.SaveChanges();

            return(context);
        }
        private void CreateHostRoleAndUsers()
        {
            // Admin role for host

            var adminRoleForHost = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin);

            if (adminRoleForHost == null)
            {
                adminRoleForHost = _context.Roles.Add(new Role(null, StaticRoleNames.Host.Admin, StaticRoleNames.Host.Admin)
                {
                    IsStatic = true, IsDefault = true
                }).Entity;
                _context.SaveChanges();
            }

            // Grant all permissions to admin role for host

            var grantedPermissions = _context.Permissions.IgnoreQueryFilters()
                                     .OfType <RolePermissionSetting>()
                                     .Where(p => p.TenantId == null && p.RoleId == adminRoleForHost.Id)
                                     .Select(p => p.Name)
                                     .ToList();

            var permissions = PermissionFinder
                              .GetAllPermissions(new ContactsAuthorizationProvider())
                              .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host) &&
                                     !grantedPermissions.Contains(p.Name))
                              .ToList();

            if (permissions.Any())
            {
                _context.Permissions.AddRange(
                    permissions.Select(permission => new RolePermissionSetting
                {
                    TenantId  = null,
                    Name      = permission.Name,
                    IsGranted = true,
                    RoleId    = adminRoleForHost.Id
                })
                    );
                _context.SaveChanges();
            }

            // Admin user for host

            var adminUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == AbpUserBase.AdminUserName);

            if (adminUserForHost == null)
            {
                var user = new User
                {
                    TenantId         = null,
                    UserName         = AbpUserBase.AdminUserName,
                    Name             = "admin",
                    Surname          = "admin",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    IsActive         = true
                };

                user.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, "123qwe");
                user.SetNormalizedNames();

                adminUserForHost = _context.Users.Add(user).Entity;
                _context.SaveChanges();

                // Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id));
                _context.SaveChanges();

                _context.SaveChanges();
            }
        }
        private void CreateRolesAndUsers()
        {
            // Admin role

            var adminRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin);

            if (adminRole == null)
            {
                adminRole = _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin)
                {
                    IsStatic = true
                }).Entity;
                _context.SaveChanges();
            }

            // Grant all permissions to admin role

            var grantedPermissions = _context.Permissions.IgnoreQueryFilters()
                                     .OfType <RolePermissionSetting>()
                                     .Where(p => p.TenantId == _tenantId && p.RoleId == adminRole.Id)
                                     .Select(p => p.Name)
                                     .ToList();

            var permissions = PermissionFinder
                              .GetAllPermissions(new ContactsAuthorizationProvider())
                              .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant) &&
                                     !grantedPermissions.Contains(p.Name))
                              .ToList();

            if (permissions.Any())
            {
                _context.Permissions.AddRange(
                    permissions.Select(permission => new RolePermissionSetting
                {
                    TenantId  = _tenantId,
                    Name      = permission.Name,
                    IsGranted = true,
                    RoleId    = adminRole.Id
                })
                    );
                _context.SaveChanges();
            }

            // Admin user

            var adminUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == AbpUserBase.AdminUserName);

            if (adminUser == null)
            {
                adminUser                  = User.CreateTenantAdminUser(_tenantId, "*****@*****.**");
                adminUser.Password         = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUser, "123qwe");
                adminUser.IsEmailConfirmed = true;
                adminUser.IsActive         = true;

                _context.Users.Add(adminUser);
                _context.SaveChanges();

                // Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id));
                _context.SaveChanges();
            }
        }
Ejemplo n.º 20
0
 public void CreateContact(Contact contact)
 {
     _context.Contacts.Add(contact);
     _context.SaveChanges();
     //_contacts.Add(contact);
 }
 public void Create(Contact contact)
 {
     _context.Add(contact);
     _context.SaveChanges();
 }
Ejemplo n.º 22
0
 public Contact Create(Contact contact)
 {
     _context.Contacts.Add(contact);
     _context.SaveChanges();
     return(contact);
 }
        public ActionResult Create([Bind(Include = "ContactID, Avatar, FullName, Age, Gender, AddressOne, AddressTwo,Phone,Mobile,Email")] ContactViewModel model, FormCollection fc, HttpPostedFileBase imageOne)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View());
                }

                if (ModelState.IsValid)
                {
                    var user = db.ContactDetails.FirstOrDefault(u => u.Avatar == avatar + imageOne.FileName || u.Email == model.Email || u.FullName == model.FullName);
                    if (user != null)
                    {
                        ModelState.AddModelError("", "This Contact already exists");
                    }

                    else if (imageOne == null)
                    {
                        ModelState.AddModelError("", "Please select a photo");
                    }

                    else if (imageOne != null)
                    {
                        string   fileName = Path.GetFileName(imageOne.FileName);
                        FileInfo file     = new FileInfo(fileName);

                        if (file.Extension == ".jpg" || file.Extension == ".jpeg" || file.Extension == ".png")
                        {
                            var filePath = Path.GetFileName(imageOne.FileName);
                            var path     = Path.Combine(Server.MapPath(avatar), filePath);
                            imageOne.SaveAs(path);

                            Image image = Image.FromFile(path);

                            if (imageOne.ContentLength > 1048576)
                            {
                                ModelState.AddModelError("", "Please select only a photo which is PNG, JPEG or JPG format that has a size of less than 1MB size");
                            }

                            else if (image.Width == 256 && image.Height == 256)
                            {
                                model.Avatar = avatar + filePath;
                                db.ContactDetails.Add(model);
                                db.SaveChanges();
                                return(RedirectToAction("Index"));
                            }

                            else
                            {
                                ModelState.AddModelError("", "Please select only photos of pixel size 256 X 256 only");
                            }
                        }

                        else
                        {
                            ModelState.AddModelError("", "");
                        }
                    }
                }

                return(View(model));
            }

            catch (Exception exception)
            {
                string excep = exception.Message;
            }
            return(View(model));
        }
Ejemplo n.º 24
0
 public static void InitializeDb(ContactsDbContext db)
 {
     db.AddRange(SeedContacts());
     db.AddRange(SeedAddresses());
     db.SaveChanges();
 }
Ejemplo n.º 25
0
 public void Create(Contact contact)
 {
     _ctx.Contacts.Add(contact);
     _ctx.SaveChanges();
 }