【问题标题】:A specified Include path is not valid. The EntityType 'testDB_KYC3Model.ts_upld_doc' does not declare a navigation property with the name 'Fields' [duplicate]指定的包含路径无效。 EntityType 'testDB_KYC3Model.ts_upld_doc' 未声明名为“Fields”的导航属性 [重复]
【发布时间】:2016-01-18 08:31:22
【问题描述】:

我遇到了错误。 指定的包含路径无效。 EntityType 'testDB_KYC3Model.ts_upld_doc' 没有声明名为 'Fields' 的导航属性。

这是我的 ts_upld_doc 课程。

 public partial class ts_upld_doc
    {
        string _template;

        public ts_upld_doc()
        {
            this.tr_upld_content = new HashSet<tr_upld_content>();
        }
        public List<tr_doc_content> Fields { get; set; }

        public int upld_docid { get; set; }
        public int usr_createdby { get; set; }
        public Nullable<int> upld_clientid { get; set; }
    public virtual ICollection<tr_upld_content> tr_upld_content { get; set; }
}

这是我的 tr_doc_content 类

public partial class tr_doc_content
    {
        public int doc_contentid { get; set; }
        public int doc_typeid { get; set; }
        public string doc_contenttypelabel { get; set; }
        public string doc_ctrltype { get; set; }
        public string doc_fieldtype { get; set; }
        public Nullable<bool> doc_isrequired { get; set; }
        public Nullable<bool> doc_isactive { get; set; }

        public virtual tm_doc_type tm_doc_type { get; set; }
    }

我有更多的课程,里面写了一些函数。

public DbDrivenView(string viewName)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                throw new ArgumentNullException("viewName", new ArgumentException("View Name cannot be null"));
            }
            _viewName = viewName;
        }

        public void Render(ViewContext viewContext, TextWriter writer)
        {

            ts_upld_doc dataForm = dbContext.ts_upld_doc.Include("Fields").First(f => f.upld_employeename == _viewName);
         var sb = new StringBuilder();
            var sw = new StringWriter(sb);
            using (HtmlTextWriter htmlWriter = new HtmlTextWriter(sw))
            {
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);

                foreach (var item in dataForm.Fields)
                {
                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                    htmlWriter.WriteEncodedText(item.doc_contenttypelabel);
 htmlWriter.AddAttribute(HtmlTextWriterAttribute.Id, item.doc_ctrltype);
                    htmlWriter.AddAttribute(HtmlTextWriterAttribute.Name, item.doc_ctrltype);
                    htmlWriter.RenderEndTag();
                    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                }htmlWriter.RenderEndTag();
            }
            writer.Write(dataForm.Template.Replace("@DataFields", sb.ToString()));
        }

当我调试此代码时,我收到以下错误异常详细信息:System.InvalidOperationException:指定的包含路径无效。 EntityType 'testDB_KYC3Model.ts_upld_doc' 没有在这行代码附​​近声明一个名为 'Fields' 的导航属性。

ts_upld_doc dataForm = dbContext.ts_upld_doc.Include("Fields").First(f => f.upld_employeename == _viewName);
       Please help me in sorting out this.     

【问题讨论】:

    标签: entity-framework model-view-controller


    【解决方案1】:

    看起来实体框架不知道“字段”是外键关系。尝试在模型构建器中显式公开这种关系,例如

    ModelBuilder.Entity<ts_upld_doc>().HasMany(d => d.Fields).WithRequired();
    

    如果启用延迟加载,请务必将此集合标记为virtual

    【讨论】:

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