I'm still learning EF and I'm trying to learn how to use a ViewModel and JOIN with multiple tables. Researching I think I found how to do it but it's not working.
I have two tables, CustomerCall and CallStatus. The customer call has a Status (int) field and the CallStatus will have the display name of the status. So I need to JOIN those together.
In my research it looks like I need a Navagation property in my ViewModel and then use the .Include of EF. So, I created a FK in SQL between CustomerCall.Status and CallStatus.Id.
Here are my class and View Model
[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; }
}
Here is the EF in my repository I'm trying to use but I get an error "A specified Include path is not valid. The EntityType 'CPPCustomerCall.ViewModels.CustomerCall' does not declare a navigation property with the name '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;
}
}