Reputation: 43
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data;
using System.Data.Entity;
using WcfRestFullService.Model;
namespace WcfRestFullService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change
the class name "CustomerSevice" in code, svc and config file together.
// 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.
public class CustomerSevice : ICustomerSevice
{
MySQLEntities dc;
public CustomerSevice()
{
dc = new MySQLEntities();
}
public List<customer> GetAllCustomer()
{
var query = (from a in dc.customers
select a).Include(c=>c.customerpreference).Distinct();
List<customer> CustomersList = new List<customer>();
query.ToList().ForEach(x =>
{
CustomersList.Add(new customer
{
Id_Cus = x.Id_Cus,
FirstName_Cus = x.FirstName_Cus,
LastName_Cus = x.LastName_Cus,
PhoneNum_Cus = x.PhoneNum_Cus,
Email_Cus = x.Email_Cus,
});
});
return CustomersList;
}
public customer CustomerDetails(string Id_Cus)
{
customer Cust = new customer();
try
{
var query = (from a in dc.customers
where a.Id_Cus.Equals(Id_Cus)
select a).Distinct().FirstOrDefault();
Cust.Id_Cus = query.Id_Cus;
Cust.FirstName_Cus = query.FirstName_Cus;
Cust.LastName_Cus = query.LastName_Cus;
Cust.PhoneNum_Cus = query.PhoneNum_Cus;
Cust.Email_Cus = query.Email_Cus;
}
catch (Exception ex)
{
throw new FaultException<string>(ex.Message);
}
return Cust;
}
// DELETE
public void DeleteCustomer(string Id_Cus)
{
//MySQLEntities Cust = new MySQLEntities(); //check the file
Model.edmx->ModelContext.tt->MySQLEntitys
int k = Convert.ToInt32(Id_Cus);
customer cur = (from n in dc.customers
where n.Id_Cus == k
select n).ToList().First();
dc.Configuration.ValidateOnSaveEnabled = false;
dc.customers.Remove(cur);
dc.SaveChanges();
}
//Insert/POST
public void InsertCustomer(customer customerDataContract)
{
//MySQLEntities Cust = new MySQLEntities();
customer cust = new customer();
cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
cust.FirstName_Cus = customerDataContract.FirstName_Cus;
cust.LastName_Cus = customerDataContract.LastName_Cus;
cust.PhoneNum_Cus =
Convert.ToInt32(customerDataContract.PhoneNum_Cus);
cust.Email_Cus = customerDataContract.Email_Cus;
dc.customers.Add(cust);
dc.SaveChanges();
}
//Update/PUT
public void UpdateCustomer(customer customerDataContract)
{
//using (CustomerDataContract Cust = new CustomerDataContract())
//using (MySQLEntities Cust = new MySQLEntities())
{
int k = Convert.ToInt32(customerDataContract.Id_Cus);
customer cust = dc.customers.Where(n => n.Id_Cus ==
k).Include(a=>a.customerpreference).FirstOrDefault();
cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
cust.FirstName_Cus = customerDataContract.FirstName_Cus;
cust.LastName_Cus = customerDataContract.LastName_Cus;
cust.PhoneNum_Cus =
Convert.ToInt32(customerDataContract.PhoneNum_Cus);
cust.Email_Cus = customerDataContract.Email_Cus;
dc.SaveChanges();
}
}
}
}
}```
[enter image description here][1]
[enter image description here][2]
[enter image description here][3]
[1]: https://i.sstatic.net/zlATm.png
[2]: https://i.sstatic.net/gQju8.png
[3]: https://i.sstatic.net/6E7vw.png
Upvotes: 0
Views: 53
Reputation: 14555
Yes, you need to explicitly eager load the customer preferences, something like this:
(from a in dc.customers select a).Include(c => c.customerpreference).Distinct();
Upvotes: 1