protected void SetUpTestData(ProductDbContext context) { products = new List <Product>() { new Product { Id = new Guid(), Name = "Test Product1", Description = "This is for Unit test", Price = new decimal(1.00), DeliveryPrice = new decimal(0.99) } }; context.AddRange(products); context.SaveChanges(); productOptions = new List <ProductOption>() { new ProductOption { Id = new Guid(), ProductId = products[0].Id, Name = "Test Product Option1", Description = "This is for Unit test" } }; context.AddRange(productOptions); context.SaveChanges(); }
public void CreateOrder(OrderInput input) { if (input.Details == null || !input.Details.Any()) { throw new KnownException("订单错误找不到详情"); } var header = _mapper.Map <OrderHeader>(input); var details = _mapper.Map <List <Data.OrderDetail> >(input.Details); details.ForEach(x => x.ParentId = header.Id); header.Amount = details.Sum(x => x.Price * x.Quantity); var msg = _mapper.Map <OrderCreatedEvent>(header); msg.Details = _mapper.Map <List <Message.OrderDetail> >(details); var dbMessage = new MqMessage() { Body = JsonConvert.SerializeObject(msg, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver(), DateFormatString = "yyyy-MM-dd hh:mm:ss" }), MessageAssemblyName = typeof(OrderCreatedEvent).Assembly.GetName().Name, MessageClassFullName = msg.GetType().FullName }; _dbContext.Add(dbMessage); _dbContext.Add(header); _dbContext.AddRange(details); _dbContext.SaveChanges(); }
public static void Seed(ProductDbContext _dbContext) { _dbContext.Database.EnsureCreated(); // Look for Any existing Data if (_dbContext.Products.Any()) { return; // Database has been Seeded } var products = new Product[] { new Product { Name = "Phone", Price = 1.00m, WeightKg = 2m, Type = ProductType.Electronic }, new Product { Name = "Chair", Price = 2.00m, WeightKg = 20m, Type = ProductType.None }, new Product { Name = "Dino", Price = 3.00m, WeightKg = 1m, Type = ProductType.Toy } }; _dbContext.AddRange(products); _dbContext.SaveChanges(); }