“未声明具有名称的导航属性” - Entity Framework孙子实体

时间:2016-06-22 16:23:24

标签: c# asp.net-mvc entity-framework

我似乎无法弄清楚是什么给了我这个错误:

  

其他信息:指定的包含路径无效。该   EntityType'TMT.CallReportsModel.CallReport'未声明a   导航属性,名称为“Topics”。

型号:

    namespace CallReports2016.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations.Schema;
    public partial class CallReport
    {
        public CallReport()
        {
            this.CallReportAttachments = new HashSet<CallReportAttachment>();
            this.CallReportContacts = new HashSet<CallReportContact>();
            this.CallReportFranchises = new HashSet<CallReportFranchise>();
            this.CallReportTopics = new HashSet<CallReportTopic>();
            this.Topics = new HashSet<Topic>();
        }

        public int ID { get; set; }
        public System.DateTime Created { get; set; }
        [DisplayName("H.O. Employee")]
        public string HQEmployee { get; set; }
        public string Type { get; set; }
        [DisplayName("Group(s)")]
        public string Group { get; set; }
        [DisplayName("Contact(s)")]
        public string Contact { get; set; }
        [DisplayName("Description")]
        public string Body { get; set; }
        public string Priority { get; set; }
        public bool IsConfidential { get; set; }

        public virtual ICollection<CallReportAttachment> CallReportAttachments { get; set; }
        [DisplayName("Contact(s)")]
        public virtual ICollection<CallReportContact> CallReportContacts { get; set; }
        [DisplayName("Franchise(s)")]
        public virtual ICollection<CallReportFranchise> CallReportFranchises { get; set; }


        public int CallReportID { get; set; }
        //[ForeignKey("TopicID")]
        public virtual ICollection<CallReportTopic> CallReportTopics { get; set; }
        //[ForeignKey("ID")]
        public virtual ICollection<Topic> Topics { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
    }
}

控制器:

    namespace CallReports2016.Controllers
{
    public class CallReportsController : Controller
    {
        private CallReportsEntities _db = new CallReportsEntities();

        //GET: CallReports
        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            //var callReports = from c in _db.CallReports.Include(c => c.CallReportFranchises)
            //                  select c;

            //var callReportFranchise = _db.CallReportFranchises.Include(d => d.FranchiseNumber);

            //var topic = _db.CallReportTopics.Include(f => f.TopicID);



            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";


            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var callReports = from c in _db.CallReports.Include(p => p.Topics.Select(c => c.Description))
                              select c;

            //var callReports = from c in _db.CallReports
            //                  select c;

            if (!String.IsNullOrEmpty(searchString))
            {
                callReports = callReports.Where(c => c.HQEmployee.Contains(searchString)
                                            || c.Type.Contains(searchString));
            }

            switch (sortOrder)
            {
                case "name_desc":
                    callReports = callReports.OrderByDescending(c => c.HQEmployee);
                    break;
                case "Date":
                    callReports = callReports.OrderBy(c => c.Created);
                    break;
                case "date_desc":
                    callReports = callReports.OrderByDescending(c => c.Created);
                    break;
                default:
                    callReports = callReports.OrderByDescending(c => c.Created);
                    break;
            }
            int pageSize = 50;
            //int pageSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
            int pageNumber = (page ?? 1);
            return View(callReports.ToPagedList(pageNumber, pageSize));
        }

来自Context:

    namespace CallReports2016.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class CallReportsEntities : DbContext
    {
        public CallReportsEntities()
            : base("name=CallReportsEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<CallReport>()
                        .HasMany<Topic>(cr => cr.Topics)
                        .WithMany(crt => crt.CallReports)
                        .Map(cs =>
                        {
                            cs.MapLeftKey("Id");
                            cs.MapRightKey("Id");
                            cs.ToTable("CallReportTopic");
                        });
        }

0 个答案:

没有答案