public TModel Add(TModel modelToAdd) { var entityToAdd = _iMapper.Map <TEntity>(modelToAdd); var result = _baseDbContext.Add(entityToAdd); return(_iMapper.Map <TModel>(result.Entity)); }
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(); }
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)); }
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()); }
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; } }
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)); }
public Task InsertAsync <T>(T entity) where T : class { dbContext.Add(entity); return(Task.CompletedTask); }
public void Insert <TOEntity>(TOEntity entity) where TOEntity : class, IBaseModel { //context.Set<TOEntity>().Add(entity); context.Add(entity); }
public virtual void Add(E entity) { _context.Add(entity); _context.SaveChanges(); }
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)); }
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)); }