使用实体框架的表中的相关数据
Posted
技术标签:
【中文标题】使用实体框架的表中的相关数据【英文标题】:Related data in tables using Entity Framework 【发布时间】:2020-07-20 15:10:02 【问题描述】:我对有疑问。我有 2 个表“customer”&&“customerpreferences”,因为两个表都有客户,所以主键是“Id_Cus”。我回顾了急切的延迟加载。决定使用急切加载。我的服务在 WCF 上实现了 REST 架构。我想知道是否有必要在每个方法中编写 (Include (a => a.customerpreferences)) 。还有一个问题,在下面的截图中,我检查了我插入上述表达式的位置,但这不起作用,你能告诉我把这个表达式放在哪里吗?提前谢谢!
我的第三张图片代码
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.stack.imgur.com/zlATm.png
[2]: https://i.stack.imgur.com/gQju8.png
[3]: https://i.stack.imgur.com/6E7vw.png
【问题讨论】:
【参考方案1】:是的,您需要明确地预先加载客户偏好,如下所示:
(from a in dc.customers select a).Include(c => c.customerpreference).Distinct();
【讨论】:
是的,我做到了。数据未出现在第二个表中 您可以激活日志记录到控制台或调试以查看正在生成的查询,然后自己运行并尝试调试它 好的,谢谢你的提示。我以尝试运行它的方式编辑了我的代码。另一个问题,我需要在所有方法中规定表关系吗?或者只是在“GET”中注册以上是关于使用实体框架的表中的相关数据的主要内容,如果未能解决你的问题,请参考以下文章