TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Kirill Filobodchenko
NA
8
530
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
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).Distinct().Include(c=>c.customerpreference);
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();
}
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace
WcfRestFullService.Model
{
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.Web;
[DataContract]
public
partial
class
customerpreference
{
[DataMember]
public
int
Id_Cus {
get
;
set
; }
[DataMember]
public
int
Id_Res {
get
;
set
; }
[DataMember]
public
string
Name_Dis {
get
;
set
; }
[DataMember]
public
int
Id_Type {
get
;
set
; }
public
virtual
customer customer {
get
;
set
; }
public
virtual
order order {
get
;
set
; }
public
virtual
type_dishes type_dishes {
get
;
set
; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace
WcfRestFullService.Model
{
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.Web;
[DataContract]
public
partial
class
customer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Usage"
,
"CA2214:DoNotCallOverridableMethodsInConstructors"
)]
public
customer()
{
this
.dishesrankings =
new
HashSet<dishesranking>();
this
.orders =
new
HashSet<order>();
}
[DataMember]
public
int
Id_Cus {
get
;
set
; }
[DataMember]
public
string
FirstName_Cus {
get
;
set
; }
[DataMember]
public
string
LastName_Cus {
get
;
set
; }
[DataMember]
public
int
PhoneNum_Cus {
get
;
set
; }
[DataMember]
public
string
Email_Cus {
get
;
set
; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Usage"
,
"CA2227:CollectionPropertiesShouldBeReadOnly"
)]
public
virtual
ICollection<dishesranking> dishesrankings {
get
;
set
; }
public
virtual
customerpreference customerpreference {
get
;
set
; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Usage"
,
"CA2227:CollectionPropertiesShouldBeReadOnly"
)]
public
virtual
ICollection<order> orders {
get
;
set
; }
}
}
Reply
Answers (
1
)
Sending a parameter from a Controller to a FormModel
Post login data to extenal web api without using ajax jquery