0

I got this error when click details link , I want to make relation between tables by using .Include() and read the name of patient and test name and there is relation between the tables and Foreign Key The error :

"A specified Include path is not valid. The EntityType 'DB_A50B96_aljawdahlabModel.LAB_RESULTS' does not declare a navigation property with the name 'patient_no'."

This is LAB_RESULTS Model :

using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;

        public partial class LAB_RESULTS
        {
            public int ID { get; set; }
            public int SAMPLE_ID { get; set; }
            public int ORDER_ID { get; set; }
            public int TESTID { get; set; }
            public int GROUPID { get; set; }
            public string NORMAL_RESULT { get; set; }
            public int SAMPLE_STATUS { get; set; }
            public string EXAMINED_BY { get; set; }
            public Nullable<System.DateTime> EXAMINED_DATE { get; set; }
            public string APPROVED_BY { get; set; }
            public Nullable<System.DateTime> APPROVED_DATE { get; set; }
            public string RESULT_NUMBER { get; set; }
            public string RESULT_REPORT { get; set; }
            public string RESULT_NOTE { get; set; }
            public Nullable<int> packageid { get; set; }
            public Nullable<int> machine_id { get; set; }
            public Nullable<int> deptid { get; set; }

            [ForeignKey("patient_no")]
            public Nullable<int> patient_no { get; set; }
            public Nullable<int> custid { get; set; }
            public string REQ_FORM_NO { get; set; }
            public Nullable<int> serial { get; set; }
            public string text { get; set; }
            public string value { get; set; }
            public string packagename { get; set; }
            public string UPDATED_BY { get; set; }
            public Nullable<System.DateTime> UPDATED_DATE { get; set; }
            public Nullable<int> prio_id { get; set; }
            public Nullable<int> update_count { get; set; }
            public string REJECTED_BY { get; set; }
            public Nullable<System.DateTime> REJECTED_DATE { get; set; }
            public Nullable<int> REJECTED_REASON { get; set; }

          //  public int Patient_no { get; set; }
          //  public Patients Patients { get; set; }

           // public LabTests labtests { get; set; }
            public virtual LabTests LabTests { get; set; }
            public virtual Patients Patients { get; set; }
        }
    }

This is patient class :

using System;
    using System.Collections.Generic;

    public partial class Patients
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Patients()
        {
            this.LAB_PARA_RESULTS = new HashSet<LAB_PARA_RESULTS>();
            this.LAB_RESULTS = new HashSet<LAB_RESULTS>();
        }

        public int Patient_No { get; set; }
        public string Patient_Name { get; set; }
        public Nullable<int> Age { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public Nullable<int> Gender { get; set; }
        public string Patient_id { get; set; }
        public Nullable<int> Natid { get; set; }
        public byte[] insurance_card { get; set; }
        public byte[] ID_card { get; set; }
        public string patient_Name_Arabic { get; set; }
        public Nullable<int> catid { get; set; }
        public Nullable<System.DateTime> DateOfBirth { get; set; }
        public Nullable<System.DateTime> DateAdded { get; set; }
        public string AddedBy { get; set; }
        public string HijriBirthDate { get; set; }
        public string PCfileNo { get; set; }
        public string GregBirthDate { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<LAB_PARA_RESULTS> LAB_PARA_RESULTS { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<LAB_RESULTS> LAB_RESULTS { get; set; }
    }
}

This is the controller :

public ActionResult CashData(int id)
        {
            var tables = new Orders_Tables
            {
                LabResults = db.LAB_RESULTS
                             .Include(p=>p.patient_no)
                             .Include(t=>t.TESTID)
                             .Where(o => o.ORDER_ID == id).ToList(),
                labtests = db.LabTests.ToList(),
                patients = db.Patients.ToList()
            };
            return View(tables);
        }

This is the view :

@model AljawdahNewSite.Models.Orders_Tables
@{
    ViewBag.Title = "CashData";
    Layout = "~/Views/Shared/_LayoutPatients.cshtml";
}

<h2>CashData</h2>

<ul class="nav nav-tabs">
    <li class="nav-item">
        <a class="nav-link active" data-toggle="tab" href="#home">Laboratory Results</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-toggle="tab" href="#menu1">Departments</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-toggle="tab" href="#menu2">Incentives</a>
    </li>
</ul>
<div class="tab-content">
    <div class="tab-pane container active" id="home">
        <h3 class="text-center text-uppercase">List of Employee</h3>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().patient_no)</th>
                    <th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().NORMAL_RESULT)</th>
                    <th>@Html.DisplayNameFor(m => m.LabResults.FirstOrDefault().APPROVED_DATE)</th>
                    <th>@Html.DisplayNameFor(m => m.patients.FirstOrDefault().Patient_Name)</th>
                    <th>@Html.DisplayNameFor(m => m.labtests.FirstOrDefault().TestName)</th>

                </tr>
            </thead>
            @foreach (var employee in Model.LabResults)
            {
                <tr>
                    <td>@employee.patient_no</td>
                    <td>@employee.NORMAL_RESULT</td>
                    <td>@employee.APPROVED_DATE</td>
                    <td>@employee.Patients.Patient_Name</td>
                    <td>@employee.LabTests.TestName</td>

                </tr>
            }
        </table>
    </div>
    </div>

I checked the solutions but its different case , I checked this link :

A specified Include path is not valid. The EntityType does not declare a navigation property with the name *

How to solve this error the error appeared in the controller ?

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20
Abdullah
  • 983
  • 12
  • 26
  • you can't include column, just entities,i think `LabResults = db.LAB_RESULTS.Where(o => o.ORDER_ID == id).ToList()` is sufficient. check [doc](https://learn.microsoft.com/en-us/ef/ef6/querying/related-data). and you can use : `LabResults = db.LAB_RESULTS .Where(o => o.ORDER_ID == id) .Include(p=>p.LabTests) .Include(t=>t.Patients) .ToList()` – Mohammed Sajid May 30 '20 at 19:27
  • @Sajid second one working thank you please add answer : LabResults = db.LAB_RESULTS .Where(o => o.ORDER_ID == id) .Include(p=>p.LabTests) .Include(t=>t.Patients) .ToList() – Abdullah May 30 '20 at 19:50
  • i have added an answer, i'm glad to help. – Mohammed Sajid May 30 '20 at 20:16

1 Answers1

1

Like mentioned in comment, you couldn't include properties, just entities, the result will be like :

LabResults = db.LAB_RESULTS
            .Where(o => o.ORDER_ID == id)
            .Include(p=>p.LabTests)
            .Include(t=>t.Patients)
            .ToList();

Include documentation

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20
  • Thank you brother for your help , can you check this error , https://stackoverflow.com/questions/62149075/asp-net-mvc-object-reference-not-set-to-an-instance-of-an-object-with-if-stateme – Abdullah Jun 02 '20 at 10:17