【问题标题】:A specified Include path is not valid. The EntityType 'DB_A50B96_aljawdahlabModel.LAB_RESULTS'指定的包含路径无效。 EntityType 'DB_A50B96_aljawdahlabModel.LAB_RESULTS'
【发布时间】:2020-05-30 18:13:52
【问题描述】:

单击详细信息链接时出现此错误,我想使用 .Include() 建立表之间的关系并读取患者姓名和测试名称,并且表和外键之间存在关系 错误:

“指定的包含路径无效。EntityType 'DB_A50B96_aljawdahlabModel.LAB_RESULTS' 未声明名为 'patient_no' 的导航属性。”

这是 LAB_RESULTS 模型:

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; }
        }
    }

这是病人课:

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; }
    }
}

这是控制器:

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);
        }

这是视图:

@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>

我检查了解决方案,但情况不同,我检查了这个链接:

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

控制器出现这个错误怎么解决?

【问题讨论】:

  • 你不能包括列,只是实体,我认为LabResults = db.LAB_RESULTS.Where(o =&gt; o.ORDER_ID == id).ToList() 就足够了。检查doc。你可以使用:LabResults = db.LAB_RESULTS .Where(o =&gt; o.ORDER_ID == id) .Include(p=&gt;p.LabTests) .Include(t=&gt;t.Patients) .ToList()
  • @Sajid 第二个工作谢谢,请添加答案:LabResults = db.LAB_RESULTS .Where(o => o.ORDER_ID == id) .Include(p=>p.LabTests) .Include( t=>t.Patients) .ToList()
  • 我已经添加了答案,我很乐意提供帮助。

标签: c# asp.net-mvc linq model-view-controller


【解决方案1】:

就像评论中提到的,你不能包含属性,只是实体,结果会是这样的:

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

Include documentation

【讨论】:

猜你喜欢
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多