EntityType Exception mapping and metadata information could not be found.

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

Question

When I attempt to save new records using the Microsoft Entity Framework and the ASP.NET MVC Framework 2, I get the following error:

A mapping and metadata record for the entity type "WebUI.Controllers.PersonViewModel" could not be located.

My view is strongly typed with class PersonViewModel, which inherits from Person, and my Entity Framework container holds records of type Person. Up until I attempted to utilize the derived view model class, records would save correctly. Can somebody explain why, when I derive my view model, the metadata class doesn't work? Without combining my storage logic (EF classes) and display logic, I want to be able to leverage a highly typed model and data annotations (metadata) (views).

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

Here is the stack trace for Update:

[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  

1
8
5/5/2011 8:47:42 AM

Popular Answer

It was a straightforward issue for me since I had named my database table UserAccount by mistake while the value in the *.edmx file was UserAccounts. HTH.

1
11/7/2013 4:24:24 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow