4

When I try to add a 'MOVIES" controller, it errors: "There was an error generating 'MvcMovieSF.Models.MovieDBContext. Try rebuilding your project."

I have rebuilt the project and it continues to error. Thanks in advance!

namespace MvcMovieSF.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet Movies { get; set; }
    }
}

JUST NOTICED: I'm not sitting at my own computer and I noticed SQLExpress services were not started; I don't have permission on this computer to start the service. Could that be the reason?

5 Answers 5

9

Adding a controller also modifies your context class. If you have implemented partial class to add some business rules into your context, the framework cannot successfully edit your partial class to find the correct points for its own insertions.

Stupid, though, removing the business rules might help. After creating your controller, you can put them back.

(at least, this worked for me, 10 minutes ago...)

Edit:

The Context class might be partially implemented like this:

public partial class MyDatabaseEntities : DbContext
{
    public override int SaveChanges() { ... }

    #region Some Business Rules Here 
    ...
    #endregion
}

Here, if you try to add a new controller, you will fail with the error: "There was an error generating 'MyDatabaseEntities'. Try rebuilding your project." And rebuilding will not help at all...

My solution is:

  1. Remove these business rules like this, of course, keep them in a safe place, as you will probably need them afterwards:

    public partial class MyDatabaseEntities : DbContext
    {
        public override int SaveChanges() { ... }
    }
    
  2. Create your controller. You should be successful this time.

  3. Remove the codes that are added by the framework from your context class:

    public partial class MyDatabaseEntities : DbContext
    {
        public override int SaveChanges() { ... }
    
        public DbSet<MyModelClass> MyModelClass { get; set; }  // remove this line
    }
    
  4. Put the business rules back.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

This workaround still works in Visual Studio 2013 preview. I had simply added an empty public partial class MyDatabaseEntites : DbContext to the project where I planned to put my extensions. Temporarily deleting my partial fixed it.
Yup, this was my problem. Wish the error message said WHY it didnt work. I just moved all my partial stuff to extension methods.
0

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.

Comments

0

@Bolt Thunder put me on the right track. In my case, the DbContext class was modified to have IDbSet for testing, which made it happen. Changed that - problem solved.

Comments

0

my project name was MVC and i had this class

 public class download
        {

            public  int download_id { get; set; }
            public  int download_category_id { get; set; }
            public  string download_name { get; set; }
            public  int download_size { get; set; }
            public  string download_description { get; set; }
            public  string download_path { get; set; }
            public  download_category download_category { get; set; }
        }

        public class download_category
        {

            public  int download_category_id { get; set; }
            public  string download_category_name { get; set; }
        }

        public class DownloadDbContext : DbContext
        {
            public DbSet<download> downloads { get; set; }
            public DbSet<download_category> download_categorys { get; set; }
        }

and i got similar error when scaffolding(there was an error generating try rebuilding your project). i am using visual studio 2012 version 11.0.50727.1 RTMREL and reference to entity as ...\EntityFramework.5.0.0\lib\net45\EntityFramework.dll

first i divide class into three classes (three seperate *.cs files) and also use DataAnnotations in download and download_category classes to use [key] for id columns and problem solved.

my classes were as below in Models folder :

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


namespace MVC.Models

{


    public class Download
    {
        [Key]
        public int download_id { get; set; }
        public int download_category_id { get; set; }
        public string download_name { get; set; }
        public int download_size { get; set; }
        public string download_description { get; set; }
        public string download_path { get; set; }
        public Download_category download_category { get; set; }
    }

}

and

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

namespace MVC.Models

{

        public class Download_category
        {
            [Key]
            public int download_category_id { get; set; }
            public string download_category_name { get; set; }
        }

}

and

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MVC.Models

{

        public class DownloadDbContext:DbContext 
       {
        public DbSet<Download> downloads { get; set; }
        public DbSet<Download_category> download_categorys { get; set; }
       }

}

Comments

0

I had the same problem as Susan. In my case specifying the model type for the DbSet (public DbSet<Movie> Movies ) in the context class fixed the problem. I am using VS 2012 and i added the Entity Framework 5.0 package to the solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.