实体框架基于查找数据加载实体 [英] Entity Framework Load Entity based on lookup data

查看:54
本文介绍了实体框架基于查找数据加载实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以根据Lookup数据加载实体。例如,我有3个表



  • callDetails {call_duration,phoneNumber,time} //我的代码中的实体和表格
  • PhoneNumbers { phoneNumber,CustomerId,phoneType} //客户可以有多个电话,表但不是实体而是复杂类型
  • Cusomter {name,city}

在我的callDetails实体中,我希望有一个属性CallDetails.Customer,并且应该根据对phonenumber值的查找来填充此客户实体? CallDetails实体只将phoneNumber作为一个简单的字符串属性(它的类型不是PhoneNumbers
实体)




singhhome

解决方案

嗨singhhome,


根据你的描述,我建议你可以创建一个新的customCallDetails并通过Query为它分配值,如下所示:

 public class CustomCallDetails 
{
public int Id {get;组; }
// ..其他字段。
公共虚拟客户客户{get;组; }

}

#Usage:

 

var result = from p在db.callDetails中

在p.phoneNumber = pn.phoneNumber
上的db.PhoneNumbers中加入pn选择新的CustomCallDetails {

id = p.Id,

//其他字段

Customer = db.Cusomter.where(t => t.CustomerId == pn.CustomerId).FirstOrDefault()

};

祝你好运,


Cole Wu


Is there a way I can load an entity based on Lookup data. example, I have 3 tables

  • callDetails {call_duration, phoneNumber, time} // entity and table in my code
  • PhoneNumbers {phoneNumber, CustomerId, phoneType} //customer can have multiple phone, table but not an entity but complex type
  • Cusomter {name, city}

in my callDetails entity I want to have a property CallDetails.Customer, and this customer entity should be populated based on lookup on phonenumber value? CallDetails entity only has phoneNumber as a simple string property (it's not of type PhoneNumbers entity)


singhhome

解决方案

Hi singhhome,

Based on your description, I would suggest that you could create a new customCallDetails and the assign the value to it via Query, like this:

public class CustomCallDetails
    {
        public int Id { get; set; }
  //.. other fields.
        public virtual Customer Customer{ get; set; }

    }

#Usage:

var result = from p in db.callDetails

join pn in db.PhoneNumbers on p.phoneNumber = pn.phoneNumber select new CustomCallDetails{

id = p.Id,

//other field

Customer = db.Cusomter.where(t=>t.CustomerId == pn.CustomerId).FirstOrDefault()

};

Best regards,

Cole Wu


这篇关于实体框架基于查找数据加载实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆