views:

93

answers:

2

I am trying out ASP.NET MVC Framework 2 with the Microsoft Entity Framework and when I try and save new records I get this error:

Mapping and metadata information could not be found for EntityType 'WebUI.Controllers.PersonViewModel'

My Entity Framework container stores records of type Person and my view is strongly typed with class PersonViewModel which derives from Person. Records would save properly until I tried to use the derived view model class. Can anyone explain why the metadata class doesnt work when I derive my view model? I want to be able to use a strongly typed model and also use data annotations (metadata) without resorting to mixing my storage logic (EF classes) and presentation logic (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

Update: here is the stack trace:

[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

A: 

Did you find a solution or workaround. We have the same problem.

alek.net
A: 

Same problem here, please tell me if you have a solution.

Thanks :D

Filip