C# 传入字典的模型项的类型为';System.Data.Entity.DynamicProxies.Survey\u D8
我有个错误。是的 传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.Survey”-U D8226C5F5E348399740EDE08FDF0A956BDAD893915272C075AF983B0C50DA25E”, 但此词典需要类型为“SurveySpaceProject.Models.User”的模型项 我正在从数据库中进行调查,但它需要用户。我尝试了Stackoverflow的一些解决方案,但没有办法。你能帮我吗?这不是家庭作业。这是我的项目,我在一周前开始使用asp.netC# 传入字典的模型项的类型为';System.Data.Entity.DynamicProxies.Survey\u D8,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我有个错误。是的 传入字典的模型项的类型为“System.Data.Entity.DynamicProxies.Survey”-U D8226C5F5E348399740EDE08FDF0A956BDAD893915272C075AF983B0C50DA25E”, 但此词典需要类型为“SurveySpaceProject.Models.User”的模型项 我正在从数据库中进行调查,但它需要用户。我尝试了Stackoverflow的一些解决方案,但没有办法。你能帮我吗?这不是家庭作业。这是我的项目
My View
public ActionResult FillSurvey(int id)
{
var s = SSPEntity.getDB().Surveys.FirstOrDefault(x => x.ID == id);
return View(s);
}
我的调查页面:
@model SurveySpaceProject.Models.Survey
@{
ViewBag.Title = "FillSurvey";
}
<h2>FillSurvey</h2>
<div>
<h4>Survey</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.User.UserName)
</dt>
<dd>
@Html.DisplayFor(model => model.User.UserName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CreatedDate)
</dt>
<dd>
@Html.DisplayFor(model => model.CreatedDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.IsDeleted)
</dt>
<dd>
@Html.DisplayFor(model => model.IsDeleted)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
@model SurveySpaceProject.Models.Survey
@{
ViewBag.Title=“FillSurvey”;
}
填充物测量
调查
@DisplayNameFor(model=>model.User.UserName)
@DisplayFor(model=>model.User.UserName)
@DisplayNameFor(model=>model.Title)
@DisplayFor(model=>model.Title)
@DisplayNameFor(model=>model.CreatedDate)
@DisplayFor(model=>model.CreatedDate)
@DisplayNameFor(model=>model.IsDeleted)
@DisplayFor(model=>model.IsDeleted)
@ActionLink(“编辑”,“编辑”,新的{id=Model.id})|
@ActionLink(“返回列表”、“索引”)
还有我的调查模型
namespace SurveySpaceProject.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Survey
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Survey()
{
SurveyAnswers = new HashSet<SurveyAnswer>();
SurveyQuestions = new HashSet<SurveyQuestion>();
//SurveyUsers = new HashSet<SurveyUser>();
}
public int ID { get; set; }
public int UserID { get; set; }
[Required]
[StringLength(150)]
public string Title { get; set; }
[Column(TypeName = "date")]
public DateTime? CreatedDate { get; set; }
public bool IsDeleted { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SurveyAnswer> SurveyAnswers { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SurveyQuestion> SurveyQuestions { get; set; }
public virtual User User { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SurveyUser> SurveyUsers { get; set; }
}
}
namespace SurveySpaceProject.Models
{
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity.Spatial;
公共部分班级调查
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公众调查()
{
SurveyAnswers=newhashset();
SurveyQuestions=新哈希集();
//SurveyUsers=newhashset();
}
公共int ID{get;set;}
public int UserID{get;set;}
[必需]
[长度(150)]
公共字符串标题{get;set;}
[列(TypeName=“日期”)]
公共日期时间?CreatedDate{get;set;}
公共布尔被删除{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection SurveyAnswers{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection调查问题{get;set;}
公共虚拟用户用户{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection SurveyUsers{get;set;}
}
}
您有两个问题:
第1期
传递给视图的分部类和模型位于命名空间命名空间SurveySpaceProject中。模型
,但查询返回的调查
位于另一个命名空间中。因此,它们是不同的类型。确保铭牌相同。这就是错误指示的内容
第二期
您必须急切地加载用户
属性。像这样使用包括:
var s = SSPEntity.getDB().Surveys.Include(x =>
x.User).FirstOrDefault(x => x.ID == id);
现在EF将生成一个查询,因此用户
信息也包含在查询中 你从哪里得到的错误?您发布的视图使用Survey
作为其模型。获得此特定异常的唯一方法是,如果实际使用的视图的模型为User
。换句话说,您要么没有加载您认为是的视图,要么没有发布重要的实际视图的代码。解释错误,但您没有向我们显示正确的代码(您所显示的不会生成该错误)。该错误清楚地表明您正在调用具有@model SurveySpaceProject.Models.User
(而不是如您所示的Survey
)的视图。如果我通过了除Survey以外的任何操作,则错误表明我需要调用“Survey”。但若我以模型的形式通过调查,它会指出错误。这在两个方面都是不正确的。1) 您所指的“其他”名称空间是System.Data.Entity.DynamicProxies
,它是动态代理EF生成的来源。但是,这些代理类继承自它们代理的实体,因此可以互换使用。2) 您不必使用Include
。OP在初始查询中包含相关用户可能更有效,但延迟加载(即不使用include
)是一个完全有效的选择。1)分部类必须与创建的类具有相同的命名空间。2) 通过延迟加载,如果查询尚未执行,它将加载属性。在FirstOrDefault之后,所有操作都在内存中完成,因此您确实需要包括。抱歉,但这两方面您都错了。分部类可以分布在多个名称空间中。只要引用了每个名称空间,编译器就会收集所有名称空间。在第二点上,访问属性将发出另一个查询。这就是延迟加载的工作原理,也是EF首先创建代理类8n的全部原因:因此它可以覆盖属性以添加此功能。您不必首先包含。1)您能提供一些关于分部类在diff程序集中的证据吗?我经常使用EF,需要使名称空间保持相同。阅读2) 如果在执行查询后访问属性,则EF不会发出另一个查询。它唯一能做到的就是如果你