2

I have a simple class called Applicant. I'm trying to add a template controller using the Entity Framework with Applicant as my model class, and a new data context.

Every time I try to create the controller, I get an error dialog that says "Unable to retrieve metadata for 'MyNameSpace.Models.Applicant'. There was an error generating 'ScaffoldingConnectionFactory'. Try rebuilding your project."

Rebuilding does nothing.

Here is my model class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyNameSpace.Models
{
    public class Applicant
    {
        public int ApplicantId { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string MiddleInitial { get; set; }
        [Required]
        public string Phone { get; set; }
        [Required]
        public string EmailAddress { get; set; }
        [Required]
        [DataType(DataType.Date)]
        public string DateOfBirth { get; set; }
        public virtual Ethnicity Ethnicity { get; set; }
        public virtual Race Race { get; set; }
    }

    public class Ethnicity 
    {
        public int EthnicityId { get; set; }
        public string Type { get; set; }
    }
    public class Race 
    {
        public int RaceId { get; set; }
        public string Type { get; set; }
    }
}

I feel like I've missed a step but I can't put my finger on it.

Billy Coover
  • 3,827
  • 5
  • 36
  • 50

11 Answers11

2

I had the same problem and had to fix it similarly.

My version of EF was incompatible with the new MVC tooling. After updating EntityFramework from NuGet, and making a few updates to outdated code, everything is working fine.

Dubmun
  • 478
  • 2
  • 10
1

I had the same problem while adding new controller in ASP.NET MVC 4, I solved the problem by moving the Database.SetInitializer(new SomeDataContextInitializer()); from the constructor of the DBContext to Application_Start method in Global.asax.cs. Hope it helps.

NicolasZ
  • 119
  • 1
  • 9
  • This little gem of fixed my problem. I have a static constructor on my DbContext that set the Initializer. The initializer seeded the database on deployment. However, back in development, no one could add views. After removing the static constructor, the add view worked again. The error stated that the connection string property was not initialized and that it could not find the metadata of my model. – Jesse Seger Jan 01 '14 at 23:57
1

This is a terrible answer and I'd like to understand the root cause, but my solution was to uninstall the EntityFramework library package reference from NuGet, then re-install it. Everything works now.

Billy Coover
  • 3,827
  • 5
  • 36
  • 50
1

I had the same issue. Tried re-installing EntityFramework using NuGet.

Didn't fix anything.

I'm about to bail and try using Linq.

I thought I had this working earlier!!

http://www.asp.net/mvc/tutorials/creating-model-classes-with-linq-to-sql-cs

Sometimes it seems like the MS stack is always changing so much it's unreliable. Is that why people use Linq?

EDIT: Running web platform installer on MS Visual web designer fixed this problem.

micahhoover
  • 2,101
  • 8
  • 33
  • 53
  • I'm not sure what your issue is but don't give up. Say what you will about MS but the guys there (Scott Gu, Scott Hanselman, Phil Haack) are awesome. I guarantee that if you can get their ear, they will help you or find someone that can. Vote this up, tweet it, whatever you can to get more eyes on the problem. – Billy Coover May 03 '11 at 21:19
  • That says a lot that you know these guys by name. They sound responsive. I used web platform installer to update Visual web designer (it took overnight), and it fixed it. I'm crawling out from under my Linux stack one step at a time ... – micahhoover May 04 '11 at 05:57
  • That's great! Glad your back in the game – Billy Coover May 04 '11 at 14:57
1

I was just met this problem and then I realize that I have to choose the right one NuGet package to avoid this problem. There are two very familiar packages in the NuGet package list.

  1. The EFCodeFirst.SqlServerCompact is used with Entity Framework Feature CTP5.
  2. The EntityFramework.SqlServerCompact is used with the Entity Framework 4.1.

The "EntityFramework.SqlServerCompact" should be used if you are using Entity Framework 4.1 !!

Will Huang
  • 2,955
  • 2
  • 37
  • 90
1

Had same problem. Problem is that the context is in separate assembly.

    namespace WebJhs179.Models {

        /// <summary>
        /// This class is just wrapper to actual context. Because actual context is in separate assembly, 
        /// MVC3's controller creation fails. 
        /// Workaround: Create empty class and inherit actual context.
        /// </summary>
        public class Jhs179Context : Jhs179.Jhs179DbContext {
        }
    }
sra
  • 23,820
  • 7
  • 55
  • 89
Sami Krank
  • 11
  • 1
1

I got this problem when I tried to create a controller using the "Add Controller" dialog box, and by selecting the "Controller with read/write actions and views, using Entity framework" Template selection.

This generates code for a Controller class. By the way, this includes references to an EntityFramework Context object (such as, in my case for a PersonController, the following line: "Person person = db.People.Find(id);"). This is for a starting point only, and of course you can replace the auto-generated code, if you wish. Either way, this is why it asks for a Context class in the dialog, and why it will auto-gen a new one for you if requested.

I referenced Entity Framework using Nuget, which had the effect of referencing EntityFramework.dll v4.0. I later abstracted out the Data Access code into a different project, so then uninstalled EF through Nuget. There were some existing references to DbSet<> in the Context class, and I think I must have resolved it by getting Resharper to add a reference to get the project simply to get it to compile/build. R# added a reference to System.Data.Entity from the .Net 4 Framework library. It was at this stage that I started getting the error when trying to create new Controllers using the "Controller with read/write actions and views, using Entity framework" template.

I resolved it by (1) removing all references to System.Data.Entity; (2) installing Entity Framework through Nuget and rebuilding; (3) Creating the controller(s); (4) uninstalling EF through Nuget, removing the auto-generated Context class and replacing the autogenerated code in the controller with my own that referenced my classes from my DAL project/assembly.

Andy Thomas
  • 1,367
  • 2
  • 14
  • 30
  • To add to this - the version of EntityFramework.dll that installed with Nuget has an assembly which does not have reference to the EntityState numerator or the State property of the context.Entry() object (DbEntityEntry class), so is useless if you want to update an instance of an entity. I ended up replacing EntityFramework.dll with the latest version of System.Data.Entity (v4.0.30319) after all. – Andy Thomas Dec 01 '11 at 17:33
0

I had the same problem using EF 4.3.1. In my case it was because I had attached extra functionality to the generated DbContext using a partial class. When I removed this class from the model project, the scaffolding worked correctly.

Paul Taylor
  • 5,651
  • 5
  • 44
  • 68
0

In my case the .edmx and model classes were in a separate assembly. This was not the issue, as I was able to get the scaffolding to work correctly simply be removing a partial class that I had used to extend the generated DbContext class.

Paul Taylor
  • 5,651
  • 5
  • 44
  • 68
0

I was trying to use MVC 4 and Entity Framework 6. I finally figured out that MVC4 references the EF5 dll by default, uninstalled and installed EF6. They I got the message:

MVC Scaffolding does not support Entity Framework 6 or later. For more information please visit: [http://go.microsoft.com/fwlink/?LinkId=276833][1]

Solution was to use MVC 5 instead, as I had already written EF6 code, and the MVC app was new.

Thronk
  • 624
  • 14
  • 37
0

Can it be that the tools expect your Entity Framework EDMX to be in the same assembly as your website?
I've had the same error occuring in a project where the EDMX was in a separate assembly.
This was nessesary because we needed database access for a website as well as for a service.

Erik Oosterwaal
  • 4,272
  • 3
  • 44
  • 64