Entity Framework in Windows forms part 4

DescriptionIn this code example continues on the first three part of this series which focuses on working with Entity Framework for Windows forms. In the examples prior to this we began with viewing data in a DataGridView which is relatively easy except for sorting which introduc

C# (12.2 MB)
 
 
 
 
 
5 Star
(1)
1,449 times
Add to favorites
5/26/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • What about ICollocations?
    6 Posts | Last post May 07, 2017
    • in advance, thank you for this sample project, which was very helpful to me.
      
      what is about icollecatins, how can i deal with them?
      
      i have created an orders table which was linked to customers by 1:m relationship, and viewed the orders in a sprate datagridview in new form by using .include(cus => cus.Orders) in getcustomers method, but now i'm facing two issues, the first one i couldn't delete a customer record even when i use:
      
      public void Remove(Customer sender)
      {
      using (MyDBEntities context = new MyDBEntities())
      {
      foreach (Order o in sender.Orders)
      {
      context.Entry(o).State = EntityState.Deleted;
      }
      context.Entry(sender).State = EntityState.Deleted;
      context.SaveChanges();
      }
      }
      ..................
      
      and the second, how can i deal with Orders CURD operations from the form2 which is contains the orders datagridview.
      
      and thank you again for your spent time
      
      best regards
      
      Mohammed Ahmed
      
    • The basics for editing is in short the same pattern as what I have shown for customers. Now for the delete operations, you need to setup the relations properly in the database. The following page explains this http://stackoverflow.com/questions/6260688/how-do-i-use-cascade-delete-with-sql-server.
      
      When setting up relations, both master and detail tables must be setup for cascading deletes.
      
      ICollection can be treated as a List<T> e.g. an Order table under Customers would be 
      
      public virtual ICollection<Order> Orders { get; set; }
      
      When using it in code we do MyEbEntities.Customers.Orders.ToList();
      
    • Dear Kareninstructor
      
      Thank you for your quick response,I did just as you explained to me, and the problems resolved
      
      
      1-	On Creating Orders Table
      
      CONSTRAINT [FK_Orders_Customers] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[ Customers] ([Id]) ON DELETE CASCADE
      
      
      2-	In Customers Class
      
      public void Remove(Sim sender)
            {
               using (MyDBEntities context = new MyDBEntities())
                 {
                     foreach (Order order in sender.Orders.ToList())
            
                         {
                              context.Entry(order).State = EntityState.Deleted;   
                         }
            
                      context.Entry(sender).State = EntityState.Deleted;
      
                      context.SaveChanges();
                              }
                     }
      
      thank you again
      
      best regards
      
      Mohammed Ahmed
    • Hi Kareninstructor
      
      
      
      i have created an Orders class to use it as Costumers class it's working very well with the other form but i should pass the current customer to it to retrieve the list of orders.
      
      i don't want to hit the database twic, becuse the order list already retrived from database by bsCostumers
      i'm planning to move orders grid to customers form with order's datagridview
      
      
      i have bonded order's datagridview data source to bsCustomers.Orders.tolist()
      
      but that throw an error when customer hasn't orders 
      
      so, i have added this method to BindingSourceExtensions class but it's not work too
      
      public static List<Order> Orders(this BindingSource sender)
              {
                  return ((Customer)sender.Current).Orders.ToList();
              }
      
      could you please help me to bind the orders list from bsCustomers to it's datagridview?
      
      best regards
      
      Mohammed
      
      
      
      
      
    • I'm currently at work and busy tonight, will have to wait until tomorrow or Friday.
    • Hi Kareninstructor
      
      okay, i know it's weekend in your country, just i like to show you what i have done.
      
      1- i have used code first to create the model. 
      2- i have set db intillizer to create db if model changed, with seed sample data.
      3- i have used entitytypeconfiguration class to map my entities to database.
      4- i have configured the one-many relationship using fluent api in modelbuilder of dbcontext class.
      
      and i had used BindingList to define the navigation property, becouse i have read that datagridview can not handle icollections.
      
      Now, all your code working completely in the right way. but still could not bind OrdersDataGrigView.
      
      i hope you have a happy weekend
      
      best regards
      
      mohammed