无法找到EntityType Exception的映射和元数据信息

asp.net-mvc-2 c# entity-framework metadata viewmodel

我正在尝试使用Microsoft Entity Framework的ASP.NET MVC Framework 2,当我尝试保存新记录时,我收到此错误:

无法找到EntityType'WebUI.Controllers.PersonViewModel'的映射和元数据信息

My Entity Framework容器存储Person类型的记录,我的视图是使用派生自Person的PersonViewModel强类型的。记录将保存正确,直到我尝试使用派生的视图模型类。任何人都可以解释为什么在推导我的视图模型时元数据类不起作用?我希望能够使用强类型模型并使用数据注释(元数据),而无需混合我的存储逻辑(EF类)和表示逻辑(视图)。

// Rest of the Person class is autogenerated by the EF
[MetadataType(typeof(Person.Metadata))]
public partial class Person
{
  public sealed class Metadata
  {
    [DisplayName("First Name")]
    [Required(ErrorMessage = "Field [First Name] is required")]
    public object FirstName { get; set; }

    [DisplayName("Middle Name")]            
    public object MiddleName { get; set; }

    [DisplayName("Last Name")]
    [Required(ErrorMessage = "Field [Last Name] is required")]
    public object LastName { get; set; }
  }
}

// From the View (PersonCreate.aspx)
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewPage<WebUI.Controllers.PersonViewModel>" %>

// From PersonController.cs
public class PersonViewModel : Person
{
   public List<SelectListItem> TitleList { get; set; }
} // end class PersonViewModel

更新 :这是堆栈跟踪:

[InvalidOperationException: Mapping and metadata information could not be found for EntityType 'WebUI.Controllers.PersonViewModel'.]  

System.Data.Objects.ObjectContext.GetTypeUsage(Type entityCLRType) +11531168  

System.Data.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation) +195  

System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity) +243  

DomainModel.Entities.MyEntities.AddToPeople(Person person) in C:\Users\...\Documents\Visual Studio 2010\Projects\PersonWeb\DomainModel\Entities\MyEntities.Designer.cs:71  

DomainModel.Concrete.Repository.SavePerson(Person person) in C:\Users\...\Documents\Visual Studio 2010\Projects\PersonWeb\DomainModel\Concrete\Repository.cs:42  

WebUI.Controllers.PersonController.Create(FormCollection form, Int32 hidCancel) in C:\Users\...\Documents\Visual Studio 2010\Projects\PersonWeb\WebUI\Controllers\PersonController.cs:163
   lambda_method(Closure , ControllerBase , Object[] ) +165  

System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +258  

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39  

System.Web.Mvc.c__DisplayClassd.b__a() +125  

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +640  

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +312  

System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709  

System.Web.Mvc.Controller.ExecuteCore() +162  

System.Web.Mvc.c__DisplayClass8.b__4() +58  

System.Web.Mvc.Async.c__DisplayClass1.b__0() +20  

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453  

System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371  

热门答案

对我来说这是一个简单的问题,当* .edmx文件中的值是UserAccounts时,我错误地在我的数据库中调用了我的表UserAccount。 HTH。



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因