public async Task Create(InventoryWeavingDocument model) { //var bonCheck = this.DbSetDoc.FirstOrDefault(s => s.Date.Date == model.Date.Date && s.BonType == model.BonType && s.Type == "OUT"); // var bonCheck = this.DbContext.InventoryWeavingDocuments.Where(s => s.Date.Date == model.Date.Date && s.BonType == model.BonType && s.Type == "OUT").FirstOrDefault(); // if (bonCheck == null) //{ model.BonNo = GenerateBon(model.BonType, model.Date); model.FlagForCreate(IdentityService.Username, UserAgent); model.FlagForUpdate(IdentityService.Username, UserAgent); foreach (var item in model.Items) { item.FlagForCreate(IdentityService.Username, UserAgent); item.FlagForUpdate(IdentityService.Username, UserAgent); } DbSetDoc.Add(model); var result = await DbContext.SaveChangesAsync(); foreach (var item in model.Items) { InventoryWeavingMovement movement = new InventoryWeavingMovement { ProductOrderName = item.ProductOrderName, BonNo = model.BonNo, ReferenceNo = item.ReferenceNo, Construction = item.Construction, Grade = item.Grade, //Piece = item.Piece, MaterialName = item.MaterialName, WovenType = item.WovenType, Width = item.Width, Yarn1 = item.Yarn1, Yarn2 = item.Yarn2, YarnType1 = item.YarnType1, YarnType2 = item.YarnType2, YarnOrigin1 = item.YarnOrigin1, YarnOrigin2 = item.YarnOrigin2, UomId = item.UomId, UomUnit = item.UomUnit, Quantity = item.Quantity, QuantityPiece = item.QuantityPiece, ProductRemark = item.ProductRemark, Type = model.Type, InventoryWeavingDocumentId = model.Id, InventoryWeavingDocumentItemId = item.Id }; movement.FlagForCreate(IdentityService.Username, UserAgent); movement.FlagForUpdate(IdentityService.Username, UserAgent); DbSetMovement.Add(movement); } var result2 = await DbContext.SaveChangesAsync(); }
public async Task <int> Create(InventoryWeavingMovement model, string username) { int Created = 0; using (var transaction = DbContext.Database.BeginTransaction()) { try { model.FlagForCreate(username, USER_AGENT); model.FlagForUpdate(username, USER_AGENT); DbSet.Add(model); Created = await DbContext.SaveChangesAsync(); } catch (Exception e) { transaction.Rollback(); throw new Exception("Insert Error : " + e.Message); } } return(Created); }