C# 向表字段显示其他数据-实体框架4
我有电话号码表:C# 向表字段显示其他数据-实体框架4,c#,.net,entity-framework,C#,.net,Entity Framework,我有电话号码表: Call(id, reason_id, company_id, text) 我有理由: 我有公司表: Calls具有Reason和Company的外键 我使用的是实体框架4,我想显示一个呼叫列表,并为每个呼叫显示文本、原因名称和公司名称 类似于内部连接的东西 我已将这些表添加到edmx文件中。如何获取所需的数据?哪个POCO对象将保存外部数据公司名称和原因名称?尝试以下操作: using (YourEntities ctx = new YourEntities()) {
Call(id, reason_id, company_id, text)
我有理由:
我有公司表:
Calls具有Reason和Company的外键
我使用的是实体框架4,我想显示一个呼叫列表,并为每个呼叫显示文本、原因名称和公司名称
类似于内部连接的东西
我已将这些表添加到edmx文件中。如何获取所需的数据?哪个POCO对象将保存外部数据公司名称和原因名称?尝试以下操作:
using (YourEntities ctx = new YourEntities())
{
//In this example, "Reason" and "Company" are the name of
//your navigation properties
return ctx.Calls.Include("Reason").Include("Company").ToList();
}
这会给你一个清单。因此,根据您的型号,每次调用都会有Call.Reason.Name或类似的名称。也许您可以使用ViewModel的概念:
//Create CallViewModel contains ReasonName & CompanyName
public class CallViewModel
{
public int id { get; set; }
public string Text{ get; set; }
public string ReasonName { get; set; }
public string CompanyName { get; set; }
}
public List<CallViewModel> YourMethid()
{
using (Entities _entities = new Entities())
{
var result = (from s in _entities.Call.Include("Reason").Include("Company")
select new CallViewModel
{
id = s.id,
Text = s.Text,
CompanyName = s.Company.name,
ReasonName = s.Reason.name
}).ToList();
return result;
}
}
=======================
//从\u实体调用表中选择所有数据
var结果=从s in_entities.Call.IncludeReason.IncludeCompany选择s.ToList
//获取第一个数据的名称
var ReasonName=result[0]。Reason.ReasonName
//获取第一个数据的CompanyName
var CompanyName=result[0]。Company.CompanyName 如果我有延迟加载,我需要包括吗?公司实际上有很多数据,但我只需要公司名称。此外,我需要将数据传递给客户端,并且我需要尽可能少的数据。Entity Framework不允许您指定要检索的列和要跳过的列。只有当对象是活动连接时,延迟加载才会起作用,因此您需要使用.Include,将对象水合,然后将其传递给客户端。如果希望它们尽可能薄,请删除延迟加载并使用POCO对象。@Maidot:我应该将这些值放在哪里?哪个POCO将保存附加信息?Naor:您使用什么样的控件来显示数据?如果您使用Include~@Naor,yae-POCO实体将保留该值:抱歉,这是我的错误,延迟加载可能无法使用Include@Maidot当前位置查看我写给BrandonZeider的评论。我需要尽可能少的数据。@Naor:为什么不使用Ajax来获取您想要的信息呢?
using (YourEntities ctx = new YourEntities())
{
//In this example, "Reason" and "Company" are the name of
//your navigation properties
return ctx.Calls.Include("Reason").Include("Company").ToList();
}
//Create CallViewModel contains ReasonName & CompanyName
public class CallViewModel
{
public int id { get; set; }
public string Text{ get; set; }
public string ReasonName { get; set; }
public string CompanyName { get; set; }
}
public List<CallViewModel> YourMethid()
{
using (Entities _entities = new Entities())
{
var result = (from s in _entities.Call.Include("Reason").Include("Company")
select new CallViewModel
{
id = s.id,
Text = s.Text,
CompanyName = s.Company.name,
ReasonName = s.Reason.name
}).ToList();
return result;
}
}