【发布时间】:2016-05-04 14:11:53
【问题描述】:
我仍在学习 EF,并且正在尝试学习如何使用 ViewModel 和 JOIN 与多个表。研究我想我找到了如何做到这一点,但它不起作用。
我有两个表,CustomerCall 和 CallStatus。客户呼叫有一个 Status (int) 字段,CallStatus 将具有状态的显示名称。所以我需要把它们一起加入。
在我的研究中,我的 ViewModel 中似乎需要一个 Navagation 属性,然后使用 .Include 的 EF。因此,我在 CustomerCall.Status 和 CallStatus.Id 之间的 SQL 中创建了一个 FK。
这是我的班级和视图模型
[Table("CustomerCall")]
public partial class CustomerCall
{
public int Id { get; set; }
[StringLength(50)]
public string CustomerName { get; set; }
[StringLength(50)]
public string Subject { get; set; }
[Column(TypeName = "text")]
public string Comment { get; set; }
public DateTime? CallDate { get; set; }
public int? Status { get; set; }
public int? AssignedTo { get; set; }
public DateTime? CreateDate { get; set; }
}
public partial class CallStatus
{
public int Id { get; set; }
[StringLength(25)]
public string StatusName { get; set; }
}
public class CustomerCallVM
{
public int Id { get; set; }
[DisplayName("Customer Name")]
public string CustomerName { get; set; }
public string Subject { get; set; }
public string Comment { get; set; }
[DisplayName("Call Date")]
public DateTime? CallDate { get; set; }
public int? Status { get; set; }
[DisplayName("Status")]
public string StatusName { get; set; }
public int? AssignedTo { get; set; }
[DisplayName("Assigned To")]
public string AssignedToName { get; set; }
[DisplayName("Create Date")]
public DateTime? CreateDate { get; set; }
public CallStatus CallStatus { get; set; }
}
这是我正在尝试使用的存储库中的 EF,但我收到错误消息“指定的包含路径无效。EntityType 'CPPCustomerCall.ViewModels.CustomerCall' 未声明名为“CallStatus”的导航属性'"
public CustomerCallVM SelectById(int? id)
{
using (DataContext db = new DataContext())
{
db.Configuration.AutoDetectChangesEnabled = false; //no changes needed so turn off for performance.
CustomerCallVM customerCall = new CustomerCallVM();
var call = db.CustomerCalls.Include("CallStatus").Where(c => c.Id == id).FirstOrDefault();
return customerCall;
}
}
【问题讨论】:
标签: c# asp.net-mvc entity-framework