Wednesday, April 19, 2017

Entity Framework Navigation Properties are not listed while creating a converter

Leave a Comment

I have following working Entity Frameowrk code for creating DTO from entity framework result. It created InvoiceDTO object that has a List<ServiceChargeDTO> property inside it. This is done referring to Mapping Linq Query results to a DTO class

public class InvoiceDTO {     public int InvoiceID { get; set; }     public List<ServiceChargeDTO> ServiceChargeLineItems { get; set; }     public decimal InvoiceTotal { get; set; } }   public InvoiceDTO GetInvoiceByID(int invoiceIDParam) {     InvoiceDTO invoice=null;     using (var db = new PortalEntities())     {         var invoices = from a in db.Invoices                        where a.InvoiceID == invoiceIDParam                        select new InvoiceDTO                        {                            InvoiceID = a.InvoiceID,                            InvoiceTotal = a.InvoiceAmount,                            ServiceChargeLineItems =                                 (from b in db.InvoiceServiceXrefs                                  where a.InvoiceID == b.InvoiceID                                  select new ServiceChargeDTO                                 {                                     ServiceChargeID = b.ServiceChargeID,                                     Quantity = b.ServiceCharge.Qty,                                     UnitPrice=b.ServiceCharge.UnitPrice,                                     Amount=b.ServiceCharge.Amount                                 }                                 ).ToList()                        };          invoice = invoices.FirstOrDefault();     }      return invoice; } 

Here data from related tables is successfully retrieved.

InvoiceServiceXrefs.ServiceCharge.Qty 

I need to change this approach and make it convert method.

public InvoiceDTO GetInvoiceByID(int invoiceIDParam) {     var invoice2 = null;     using (var db = new PortalEntities())     {         var invoices2 = from b in db.Invoices                         where b.InvoiceID == invoiceIDParam                         select b;         invoice2 = ToInvoiceDTO(invoices2.FirstOrDefault());      }      return invoice2; } 

But the navigation properties are not getting listed when I type invoice.InvoiceServiceXrefs..

enter image description here

How to get the navigation properties correctly and create InvoiceDTO object with List<ServiceChargeDTO>?

3 Answers

Answers 1

It looks like you need to map your List<InvoiceServiceXrefs> to List<ServiceChargeDTO>.

private static InvoiceDTO ToInvoiceDTO(Invoice invoice) {     InvoiceDTO e = new InvoiceDTO()     {         InvoiceID = invoice.InvoiceID,         InvoiceTotal = invoice.TotalAmount     };      foreach(var invRef in invoice.InvoiceServiceXrefs)     {         e.ServiceChargeLineItems.Add(new ServiceChargeDTO         {             ServiceChargeID = invRef.ServiceChargeID,             Quantity = invRef.Qty,             UnitPrice = invRef.UnitPrice,             Amount = invRef.Amount         });     }      return e; } 

Answers 2

You can use pretty much the same code to map your ServiceChargeLineItems, that you used in your initial query

private static InvoiceDTO ToInvoiceDTO(Invoice invoice) {     InvoiceDTO e = new InvoiceDTO()     {         InvoiceID = invoice.InvoiceID,         InvoiceTotal = invoice.TotalAmount     };      e.ServiceChargeLineItems = invoice.InvoiceServiceXrefs.                                Select(b => new ServiceChargeDTO                                {                                    ServiceChargeID = b.ServiceChargeID,                                    Quantity = b.ServiceCharge.Qty,                                    UnitPrice = b.ServiceCharge.UnitPrice,                                    Amount = b.ServiceCharge.Amount                                }).ToList()      return e; } 

Answers 3

When you ran LINQ query on db.Invoices in your GetInvoiceByID method, it became an IQuerable variable invoices2. If you do not want to do perform nested LINQ query in one shot, better return them as DbSet invoices2 and use it to create your desired DTO like this

public DbSet"<"Invoices">" GetInvoiceByID(int invoiceIDParam) {      var invoice2 = null;      using (var db = new PortalEntities())      {           var invoices2 = from b in db.Invoices                           where b.InvoiceID == invoiceIDParam                           select b;           //REMOVE THIS invoice2 = ToInvoiceDTO(invoices2.FirstOrDefault());       }        return invoice2; } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment