Exemplo n.º 1
0
        public void Update(Seller seller)
        {
            using (var context = DataContext.CreateNew())
            {
                context.Database.Log = Console.WriteLine;

                foreach (var item in seller.Properties)
                {
                    if(context.SellerProperties.FirstOrDefault(x=>x.SellerId == item.SellerId) !=null)
                    {
                        context.SellerProperties.Attach(item);
                        context.Entry(item).State = EntityState.Modified;

                    }
                    else
                    {
                        context.SellerProperties.Add(item);
                        context.Entry(item).State = EntityState.Added;
                    }
                }
                context.Sellers.Attach(seller);
                context.Entry(seller).State = EntityState.Modified;
                context.SaveChangesAsync();
            }
        }
Exemplo n.º 2
0
 public void Remove(Seller seller)
 {
     using (var context = DataContext.CreateNew())
     {
         context.Sellers.Remove(seller);
         context.SaveChangesAsync();
     }
 }
Exemplo n.º 3
0
 public int Add(Seller seller)
 {
     using (var context = DataContext.CreateNew())
     {
         context.Database.Log = Console.WriteLine;
         context.Sellers.Add(seller);
         context.SaveChanges();
         return seller.SellerId;
     }
 }
Exemplo n.º 4
0
        public void InsertOrUpdate(Seller seller)
        {
            using (var context = DataContext.CreateNew())
            {

                if (seller.SellerId == default(int))
                {
                    context.Sellers.Add(seller);
                }
                else
                {
                    UpdateSeller(seller, context);
                }
                context.SaveChanges();
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var s = new SellerRepository();

            var seller = new Seller { Name = "Flipkart", Url = "http://flipkart.com" };

            seller.Properties = new List<SellerProperty>
                                {
                                    new SellerProperty{Name = "Flipkart Shopping Type new ", Value = "E Commerse site"},
                                    new SellerProperty{Name = "Flipkart Address new asdfasd", Value = "Our favorite address"}
                                };

            s.InsertOrUpdate(seller);

            //var a = s.All;

            //s.Delete(a.FirstOrDefault().SellerId);
        }
Exemplo n.º 6
0
 private void PrepareObjectGraph(Seller seller, DataContext context)
 {
     context.Sellers.Add(seller);
 }
Exemplo n.º 7
0
 private void MarkSellerAsModified(Seller seller, DataContext context)
 {
     context.Entry(seller).State = EntityState.Modified;
 }
Exemplo n.º 8
0
 private void MarkAllPropertiesAsNew(Seller seller, DataContext context)
 {
     seller.Properties.ForEach(x => context.Entry(x).State = EntityState.Added);
 }
Exemplo n.º 9
0
 private void DeleteExistingProperties(Seller seller, DataContext context)
 {
     context.Database.ExecuteSqlCommand("DELETE from SellerProperties where SellerId = " + seller.SellerId);
 }
Exemplo n.º 10
0
 private void UpdateSeller(Seller seller, DataContext context)
 {
     PrepareObjectGraph(seller, context);
     MarkSellerAsModified(seller, context);
     DeleteExistingProperties(seller, context);
     MarkAllPropertiesAsNew(seller, context);
 }