Related data in tables using Entity Framework

Apr 8 2020 9:55 AM
I have a question on related data in tables using Entity Framework. I have 2 tables "customer" && "customerpreferences", since both tables have customer, then the primary key is "Id_Cus". I reviewed eager, lazy loading. It was decided to use eager loading. My service implements the REST architecture on WCF. I would like to know whether it is necessary to write (Include (a => a.customerpreferences)) in each of the methods. And another question, in the screenshots attached below, I checked out where I inserted the expression described above, but this did not work, could you tell me where to put this expression. Thank you in advance!
 
My code for 3rd image
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7. using MySql.Data;  
  8. using System.Data.Entity;  
  9.   
  10. using WcfRestFullService.Model;  
  11.   
  12. namespace WcfRestFullService  
  13. {  
  14.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerSevice" in code, svc and config file together.  
  15.     // NOTE: In order to launch WCF Test Client for testing this service, please select CustomerSevice.svc or CustomerSevice.svc.cs at the Solution Explorer and start debugging.  
  16.     public class CustomerSevice : ICustomerSevice  
  17.     {  
  18.         MySQLEntities dc;  
  19.         public CustomerSevice()  
  20.         {  
  21.             dc = new MySQLEntities();  
  22.         }  
  23.   
  24.         public List<customer> GetAllCustomer()  
  25.         {  
  26.             var query = (from a in dc.customers  
  27.                          select a).Distinct().Include(c=>c.customerpreference);  
  28.   
  29.             List<customer> CustomersList = new List<customer>();  
  30.   
  31.             query.ToList().ForEach(x =>  
  32.             {  
  33.                 CustomersList.Add(new customer  
  34.                 {  
  35.                     Id_Cus = x.Id_Cus,  
  36.                     FirstName_Cus = x.FirstName_Cus,  
  37.                     LastName_Cus = x.LastName_Cus,  
  38.                     PhoneNum_Cus = x.PhoneNum_Cus,  
  39.                     Email_Cus = x.Email_Cus,  
  40.                 });  
  41.             });  
  42.             return CustomersList;  
  43.         }  
  44.   
  45.         public customer CustomerDetails(string Id_Cus)  
  46.         {  
  47.             customer Cust = new customer();  
  48.             try  
  49.             {  
  50.                 var query = (from a in dc.customers  
  51.                              where a.Id_Cus.Equals(Id_Cus)  
  52.                              select a).Distinct().FirstOrDefault();  
  53.                 Cust.Id_Cus = query.Id_Cus;  
  54.                 Cust.FirstName_Cus = query.FirstName_Cus;  
  55.                 Cust.LastName_Cus = query.LastName_Cus;  
  56.                 Cust.PhoneNum_Cus = query.PhoneNum_Cus;  
  57.                 Cust.Email_Cus = query.Email_Cus;  
  58.             }  
  59.             catch (Exception ex)  
  60.             {  
  61.                 throw new FaultException<string>(ex.Message);  
  62.             }  
  63.             return Cust;  
  64.         }  
  65.   
  66.         // DELETE  
  67.   
  68.         public void DeleteCustomer(string Id_Cus)  
  69.         {  
  70.             //MySQLEntities Cust = new MySQLEntities(); //check the file Model.edmx->ModelContext.tt->MySQLEntitys  
  71.   
  72.             int k = Convert.ToInt32(Id_Cus);  
  73.             customer cur = (from n in dc.customers  
  74.                             where n.Id_Cus == k  
  75.                             select n).ToList().First();  
  76.   
  77.             dc.Configuration.ValidateOnSaveEnabled = false;  
  78.             dc.customers.Remove(cur);  
  79.             dc.SaveChanges();  
  80.         }  
  81.   
  82.         //Insert/POST  
  83.   
  84.         public void InsertCustomer(customer customerDataContract)  
  85.         {  
  86.             //MySQLEntities Cust = new MySQLEntities();  
  87.             customer cust = new customer();  
  88.   
  89.             cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);  
  90.             cust.FirstName_Cus = customerDataContract.FirstName_Cus;  
  91.             cust.LastName_Cus = customerDataContract.LastName_Cus;  
  92.             cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);  
  93.             cust.Email_Cus = customerDataContract.Email_Cus;  
  94.             dc.customers.Add(cust);  
  95.             dc.SaveChanges();  
  96.         }  
  97.   
  98.         //Update/PUT  
  99.         public void UpdateCustomer(customer customerDataContract)  
  100.         {  
  101.             //using (CustomerDataContract Cust = new CustomerDataContract())  
  102.             //using (MySQLEntities Cust = new MySQLEntities())   
  103.             {  
  104.                 int k = Convert.ToInt32(customerDataContract.Id_Cus);  
  105.                 customer cust = dc.customers.Where(n => n.Id_Cus == k).Include(a=>a.customerpreference).FirstOrDefault();  
  106.   
  107.                 cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);  
  108.                 cust.FirstName_Cus = customerDataContract.FirstName_Cus;  
  109.                 cust.LastName_Cus = customerDataContract.LastName_Cus;  
  110.                 cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);  
  111.                 cust.Email_Cus = customerDataContract.Email_Cus;  
  112.   
  113.                 dc.SaveChanges();  
  114.             }  
  115.         }  
  116.     }  
  117. }  
  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. //     This code was generated from a template.  
  4. //  
  5. //     Manual changes to this file may cause unexpected behavior in your application.  
  6. //     Manual changes to this file will be overwritten if the code is regenerated.  
  7. // </auto-generated>  
  8. //------------------------------------------------------------------------------  
  9.   
  10. namespace WcfRestFullService.Model  
  11. {  
  12.     using System;  
  13.     using System.Collections.Generic;  
  14.     using System.Linq;  
  15.     using System.Runtime.Serialization;  
  16.     using System.Web;  
  17.   
  18.     [DataContract]  
  19.     public partial class customerpreference  
  20.     {  
  21.         [DataMember]  
  22.         public int Id_Cus { getset; }  
  23.         [DataMember]  
  24.         public int Id_Res { getset; }  
  25.         [DataMember]  
  26.         public string Name_Dis { getset; }  
  27.         [DataMember]  
  28.         public int Id_Type { getset; }  
  29.   
  30.         public virtual customer customer { getset; }  
  31.         public virtual order order { getset; }  
  32.         public virtual type_dishes type_dishes { getset; }  
  33.     }  
  34. }  
  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. //     This code was generated from a template.  
  4. //  
  5. //     Manual changes to this file may cause unexpected behavior in your application.  
  6. //     Manual changes to this file will be overwritten if the code is regenerated.  
  7. // </auto-generated>  
  8. //------------------------------------------------------------------------------  
  9.   
  10. namespace WcfRestFullService.Model  
  11. {  
  12.     using System;  
  13.     using System.Collections.Generic;  
  14.     using System.Linq;  
  15.     using System.Runtime.Serialization;  
  16.     using System.Web;  
  17.   
  18.     [DataContract]  
  19.     public partial class customer  
  20.     {  
  21.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]  
  22.         public customer()  
  23.         {  
  24.             this.dishesrankings = new HashSet<dishesranking>();  
  25.             this.orders = new HashSet<order>();  
  26.         }  
  27.   
  28.         [DataMember]  
  29.         public int Id_Cus { getset; }  
  30.         [DataMember]  
  31.         public string FirstName_Cus { getset; }  
  32.         [DataMember]  
  33.         public string LastName_Cus { getset; }  
  34.         [DataMember]  
  35.         public int PhoneNum_Cus { getset; }  
  36.         [DataMember]  
  37.         public string Email_Cus { getset; }  
  38.   
  39.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  40.         public virtual ICollection<dishesranking> dishesrankings { getset; }  
  41.         public virtual customerpreference customerpreference { getset; }  
  42.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  43.         public virtual ICollection<order> orders { getset; }  
  44.     }  
  45. }

Answers (1)