コード例 #1
0
        public ActionResult Delete([FromRoute] int id)
        {
            var prod = _ctx.Products.Find(id);

            _ctx.Products.Remove(prod);
            _ctx.SaveChanges();
            return(Ok());
        }
コード例 #2
0
        public void Add(Employee employee)
        {
            using (var context = new AcmeDbContext())
            {
                context.Employees.Add(employee);

                context.SaveChanges();
            }
        }
コード例 #3
0
        public void Add(ApplicationUser applicationUser)
        {
            using (var context = new AcmeDbContext())
            {
                context.ApplicationUsers.Add(applicationUser);

                context.SaveChanges();
            }
        }
コード例 #4
0
        public bool SaveSignup(Signup signup)
        {
            var existingSignup = GetSignup(signup.SignupId);

            if (existingSignup == null)
            {
                var newSignupId = CreateSignup(signup);
                return(newSignupId > 0);
            }

            #region Here we may user AutoMapper
            existingSignup.Activity = signup.Activity;
            existingSignup.Comments = signup.Comments;
            existingSignup.User     = signup.User;
            #endregion

            var retValue = _dbCtx.SaveChanges();
            if (retValue == 0)
            {
                _dbCtx.Entry(signup).State = EntityState.Modified;
            }
            return(retValue == 0);
        }
コード例 #5
0
        public bool SaveUser(User user)
        {
            var existingUser = GetUser(user.Email);

            if (existingUser == null)
            {
                var newUserId = CreateUser(user);
                return(newUserId > 0);
            }

            #region Here we may user AutoMapper
            existingUser.FirstName = user.FirstName;
            existingUser.LastName  = user.LastName;
            existingUser.Email     = user.Email;
            #endregion

            var retValue = _dbCtx.SaveChanges();
            if (retValue == 0)
            {
                _dbCtx.Entry(user).State = EntityState.Modified;
            }
            return(retValue == 0);
        }
コード例 #6
0
        public virtual void Insert(T entity)
        {
            try
            {
                if (entity == null)
                {
                    throw new ArgumentNullException(nameof(entity));
                }

                Entities.Add(entity);

                _context.SaveChanges();
            }
            catch (InvalidOperationException dbEx)
            {
                //ensure that the detailed error text is saved in the Log
                throw new Exception(dbEx.Message);
            }
        }
コード例 #7
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new AcmeDbContext(
                       serviceProvider.GetRequiredService <
                           DbContextOptions <AcmeDbContext> >()))
            {
                if (context.Submission.Any())
                {
                    return;   // DB has been seeded
                }

                for (int i = 0; i < 2; i++)
                {
                    context.Submission.AddRange(
                        new Submission
                    {
                        FullName  = "Rasmus Paludan",
                        Email     = "*****@*****.**",
                        Age       = 47,
                        SerialNum = 95676611
                    },
                        new Submission
                    {
                        FullName  = "Kaj Boyesen",
                        Email     = "*****@*****.**",
                        Age       = 45,
                        SerialNum = 32427143
                    },
                        new Submission
                    {
                        FullName  = "Søren Malling",
                        Email     = "*****@*****.**",
                        Age       = 55,
                        SerialNum = 59395710
                    },
                        new Submission
                    {
                        FullName  = "Gordon Ramsey",
                        Email     = "*****@*****.**",
                        Age       = 50,
                        SerialNum = 74457705
                    },
                        new Submission
                    {
                        FullName  = "Morgan Freeman",
                        Email     = "*****@*****.**",
                        Age       = 100,
                        SerialNum = 67800879
                    },
                        new Submission
                    {
                        FullName  = "Kanye West",
                        Email     = "*****@*****.**",
                        Age       = 48,
                        SerialNum = 65996900
                    },
                        new Submission
                    {
                        FullName  = "Egon Olsen",
                        Email     = "*****@*****.**",
                        Age       = 90,
                        SerialNum = 35581572
                    },
                        new Submission
                    {
                        FullName  = "Casper Christensen",
                        Email     = "*****@*****.**",
                        Age       = 52,
                        SerialNum = 88059526
                    },
                        new Submission
                    {
                        FullName  = "Ole Thestrup",
                        Email     = "*****@*****.**",
                        Age       = 80,
                        SerialNum = 93172768
                    },
                        new Submission
                    {
                        FullName  = "Kim Larsen",
                        Email     = "*****@*****.**",
                        Age       = 63,
                        SerialNum = 46632564
                    },
                        new Submission
                    {
                        FullName  = "Margrethe II",
                        Email     = "majestæ[email protected]",
                        Age       = 80,
                        SerialNum = 38875640
                    },
                        new Submission
                    {
                        FullName  = "Mads Mikkelsen",
                        Email     = "*****@*****.**",
                        Age       = 54,
                        SerialNum = 82514261
                    },
                        new Submission
                    {
                        FullName  = "Nikolaj-Lie Kaas",
                        Email     = "*****@*****.**",
                        Age       = 46,
                        SerialNum = 58079554
                    }
                        );
                    context.SaveChanges();
                }
            }
        }
コード例 #8
0
 public void Save()
 {
     _dbContext.SaveChanges();
 }