Beispiel #1
0
        public static void GenerateSampleData()
        {
            using (var db = new FasConDBEntities())
            {
                for (int i = 0; i < 10; i++)
                {
                    var cat = new Category
                    {
                        CategoryId = Guid.NewGuid(),
                        Name= "Danh mục " + i.ToString() ,
                        FullName = "Danh mục " + i.ToString(),
                        Alias="danh-muc-"+i.ToString()

                    };
                    try
                    {
                        db.Categories.Add(cat);
                        db.SaveChanges();
                        for (int j = 10; j < 100; j++)
                        {
                            db.Products.Add(new Product
                            {
                                ProductID = Guid.NewGuid(),
                                ProductName = "Sản phẩm " + i + j,
                                Alias="san-pham-"+i+j,
                                ProductThumb="http://sandichvu.com.vn/storage/app/uploads/public/556/889/db6/thumb_74_250x250_0_0_crop.jpg",
                                Category = cat,
                                ProductSKU="SKU "+i+j,
                                ShortDescription = "ShortDescription" +i+j,
                                Price=100000

                            });
                        }
                        db.SaveChanges();
                    }

                    catch (DbEntityValidationException e)
                    {
                        foreach (var eve in e.EntityValidationErrors)
                        {
                            Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                eve.Entry.Entity.GetType().Name, eve.Entry.State);
                            foreach (var ve in eve.ValidationErrors)
                            {
                                Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                    ve.PropertyName, ve.ErrorMessage);
                            }
                        }
                        throw;
                    }
                }

            }
        }
Beispiel #2
0
        public static Boolean UpdateAddress(AddressModel addressModel)
        {
            using (var db = new FasConDBEntities())
            {
                var address = db.Addresses.Where(p => p.AddressId == addressModel.AddressId).FirstOrDefault();
                bool isNew = false;
                if (address == null)
                {
                    isNew = true;
                    address = new Address();
                    address.AddressId = Guid.NewGuid();
                }
                address.FullName = addressModel.FullName;
                address.Email = addressModel.Email;
                address.Company = addressModel.Company;
                address.DistrictId = addressModel.DistrictId;
                address.StateProvinceId = addressModel.StateProvinceId;
                address.Street = addressModel.Street;
                address.PhoneNumber = addressModel.PhoneNumber;
                address.FaxNumber = addressModel.FaxNumber;
                address.CreatedOnUtc = addressModel.CreatedOnUtc;
                if(isNew)
                db.Addresses.Add(address);

                db.SaveChanges();

            }

            return true;
        }
Beispiel #3
0
        public static void GenerateCustomer()
        {
            using (var db = new FasConDBEntities())
            {
                db.Customers.Add(new Customer
                {
                    CustomerId = Guid.NewGuid(),
                    Name = "huu loi"

                });
                db.SaveChanges();
            }
        }
Beispiel #4
0
        public static Boolean DeleteCustomer(CustomerModel customerModel)
        {
            using (var db = new FasConDBEntities())
            {
                var order = db.Orders.Where(c => c.CustomerId == customerModel.CustomerId).FirstOrDefault();
                if (order == null)
                {
                    var customer = db.Customers.Where(c => c.CustomerId == customerModel.CustomerId).FirstOrDefault();
                    if (customer != null)
                        db.Customers.Remove(customer);
                    db.SaveChanges();
                }

            }
            return true;
        }
Beispiel #5
0
 /// <summary>
 /// Add new or update exist customer
 /// </summary>
 /// <param name="orderModel"></param>
 /// <returns></returns>
 public static Boolean UpdateCustomer(CustomerModel customerModel)
 {
     using (var db = new FasConDBEntities())
     {
         var customer = db.Customers.Where(c => c.CustomerId == customerModel.CustomerId).FirstOrDefault();
         if (customer == null)
         {
             customer = new Customer
             {
                 CustomerId = Guid.NewGuid(),
                 Name = customerModel.Name,
                 UpdatedDate = DateTime.Now,
                 UpdatedUserId = customerModel.UpdatedUserId,
                 CreatedDate = DateTime.Now,
                 CreatedUserId = customerModel.CreatedUserId,
                 StoreId = customerModel.StoreId
             };
             db.Customers.Add(customer);
         }
         else
         {
             customer.Name = customerModel.Name;
             customer.UpdatedDate = DateTime.Now;
             customer.UpdatedUserId = customerModel.UpdatedUserId;
             customerModel.Deleted = customerModel.Deleted;
             customer.StoreId = customerModel.StoreId;
         }
         db.SaveChanges();
     }
     return true;
 }
Beispiel #6
0
        public static Boolean UpdateOrder(OrderModel orderModel)
        {
            using (var db = new FasConDBEntities())
            {
                var order = db.Orders.Where(c => c.OrderId == orderModel.OrderId).FirstOrDefault();
                if (order == null)
                {
                    order = new Order
                    {
                        OrderId = orderModel.OrderId,
                        OrderNumber = orderModel.OrderNumber,
                        StoreId = orderModel.StoreId,
                        CustomerId = orderModel.CustomerId,
                        ShippingAddressId = orderModel.ShippingAddressId,
                        OrderStatusId = orderModel.OrderStatusId,
                        PaymentStatusId = orderModel.PaymentStatusId,
                        VatNumber = orderModel.VatNumber,
                        OrderSubtotal = orderModel.OrderSubtotal,
                        OrderTax = orderModel.OrderTax,
                        OrderTotal = orderModel.OrderTotal,
                        CustomerIp = orderModel.CustomerIp,
                        PaidDateUtc = orderModel.PaidDateUtc,
                        ShippingMethod = orderModel.ShippingMethod,
                        Deleted = orderModel.Deleted,
                        CreatedOnUtc = orderModel.CreatedOnUtc,
                        UpdatedOnUtc = orderModel.UpdatedOnUtc,
                        CustomerOrderComment = orderModel.CustomerOrderComment
                    };
                    db.Orders.Add(order);
                }
                else
                {
                    order.OrderId = orderModel.OrderId;
                    order.OrderNumber = orderModel.OrderNumber;
                    order.StoreId = orderModel.StoreId;
                    order.CustomerId = orderModel.CustomerId;
                    order.ShippingAddressId = orderModel.ShippingAddressId;
                    order.OrderStatusId = orderModel.OrderStatusId;
                    order.PaymentStatusId = orderModel.PaymentStatusId;
                    order.VatNumber = orderModel.VatNumber;
                    order.OrderSubtotal = orderModel.OrderSubtotal;
                    order.OrderTax = orderModel.OrderTax;
                    order.OrderTotal = orderModel.OrderTotal;
                    order.CustomerIp = orderModel.CustomerIp;
                    order.PaidDateUtc = orderModel.PaidDateUtc;
                    order.ShippingMethod = orderModel.ShippingMethod;
                    order.Deleted = orderModel.Deleted;
                    order.CreatedOnUtc = orderModel.CreatedOnUtc;
                    order.UpdatedOnUtc = orderModel.UpdatedOnUtc;
                    order.CustomerOrderComment = orderModel.CustomerOrderComment;

                }
                db.SaveChanges();
            }

            return true;
        }