public TModel Add(TModel modelToAdd)
        {
            var entityToAdd = _iMapper.Map <TEntity>(modelToAdd);
            var result      = _baseDbContext.Add(entityToAdd);

            return(_iMapper.Map <TModel>(result.Entity));
        }
Exemplo n.º 2
0
        public async Task SignupDonor(BloodDonor donor)
        {
            // TODO: Use a model class and validate client input before saving
            var existing = await _dbContext.Set <BloodDonor>()
                           .FirstOrDefaultAsync(x =>
                                                x.FirstName == donor.FirstName &&
                                                x.LastName == donor.LastName &&
                                                x.DateOfBirth == donor.DateOfBirth);

            if (existing != null)
            {
                throw new BBMSException($"{donor.FirstName}, you've signed up before. Go ahead to schedule for blood donation");
            }

            _dbContext.Add(donor);
            await _dbContext.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddOrEdit([Bind("EmployeeId,FullName,Empcode,Position,OfficeLocation")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                if (employee.EmployeeId == 0)
                {
                    _context.Add(employee);
                }
                else
                {
                    _context.Employees.Update(employee);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(employee));
        }
Exemplo n.º 4
0
    public IActionResult Index()
    {
        // get currently logged in user's username from ASP.NET Identity
        string userName = User.Identity.Name;

        // get username in Member table of baseDb where Id column is 123
        string anotherUserName = _context.Member().FirstOrDefault(m => m.Id == 123).UserName;

        // add new row in basedb.Member table with identity username as foreign key
        var model = new Member
        {
            UserName  = userName,
            Age       = 1,
            Favorites = new List <Favorite>();
        };

        _baseContext.Add(model);
        _baseContext.SaveChanges();

        return(View());
    }
Exemplo n.º 5
0
        public static void SeedOrders(BaseDbContext context)
        {
            try
            {
                if (!context.Order.Any())
                {
                    var products = context.Product.ToList();

                    var order = new Order
                    {
                        Price      = new Random().Next(10, 100),
                        CustomerID = 1,
                    };
                    context.Add(order);
                    context.SaveChanges();

                    var orderProducts = new List <OrderProduct>();
                    foreach (var product in products)
                    {
                        orderProducts.Add(new OrderProduct
                        {
                            OrderID   = order.ID,
                            ProductID = product.ID,
                            Price     = order.Price / products.Count()
                        });
                    }

                    context.AddRange(orderProducts);
                    context.SaveChanges();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create(Post posts)
        {
            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                foreach (var Image in files)
                {
                    if (Image != null && Image.Length > 0)
                    {
                        var file = Image;
                        //There is an error here
                        var uploads = Path.Combine(_appEnvironment.WebRootPath, "img\\post");
                        if (file.Length > 0)
                        {
                            var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName);
                            using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create))
                            {
                                await file.CopyToAsync(fileStream);

                                posts.Image = fileName;
                            }
                        }
                    }
                }


                posts.UserName  = userManager.GetUserName(User);
                posts.TimeStamp = DateTime.Now.Date;
                _context.Add(posts);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            GetViewBagData();
            return(View(posts));
        }
Exemplo n.º 7
0
 public Task InsertAsync <T>(T entity) where T : class
 {
     dbContext.Add(entity);
     return(Task.CompletedTask);
 }
Exemplo n.º 8
0
 public void Insert <TOEntity>(TOEntity entity) where TOEntity : class, IBaseModel
 {
     //context.Set<TOEntity>().Add(entity);
     context.Add(entity);
 }
Exemplo n.º 9
0
 public virtual void Add(E entity)
 {
     _context.Add(entity);
     _context.SaveChanges();
 }
Exemplo n.º 10
0
 public static void AddAudited <TEntity>(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, TEntity entity, string transactionMessage, string createdBy = null, string controllerName = null, string actionName = null, string remoteIpAddress = null)
     where TEntity : class
 {
     dbContext.Add(entity);
     dbContext.Add(entity.CreateAuditTrails(transactionMessage, createdBy, controllerName, actionName, remoteIpAddress));
 }
Exemplo n.º 11
0
 public static void RemoveAudited <TEntity>(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, IEnumerable <TEntity> entities, string transactionMessage, string createdBy = null, string controllerName = null, string actionName = null, string remoteIpAddress = null)
     where TEntity : class
 {
     dbContext.RemoveRange(entities);
     dbContext.Add(entities.CreateAuditTrails(transactionMessage, createdBy, controllerName, actionName, remoteIpAddress));
 }