ASP.NET MVC Controller Error

When I try to add the "MOVIES" controller, these are errors: " An error has occurred generating MvcMovieSF.Models.MovieDBContext. Try rebuilding your project .

I rebuilt the project and it continues to be mistaken. 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; } } } 

SEE ONLY: I am not sitting at my computer, and I noticed that SQLExpress services did not start; I do not have permission on this computer to start the service. Maybe this is the reason?

+4
source share
5 answers

Adding a controller also changes your context class. If you have implemented a partial class to add some business rules to your context, the infrastructure will not be able to successfully modify your partial class to find the right points for its own inserts.

Silly, however, eliminating business rules can help. After creating the controller, you can return them.

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

Edit:

The Context class can be partially implemented as follows:

 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: โ€œError generatingโ€œ MyDatabaseEntities. โ€Try rebuilding your project. And restoring will not help at all ...

My decision:

  • Delete these business rules , as of course, keep them in a safe place, since you will probably need them later:

     public partial class MyDatabaseEntities : DbContext { public override int SaveChanges() { ... } } 
  • Create your controller. This time you must be successful.

  • Remove the codes 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 } 
  • Return business rules.

Hope this helps.

+8
source

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

0
source

@Bolt Thunder set me on the right track. In my case, the DbContext class was changed to have an IDbSet for testing, which did it. Changed - the problem is resolved.

0
source

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 a similar error when creating forests (an error occurred while trying to restore your project). I am using Visual Studio 2012 version 11.0.50727.1 RTMREL and an object reference as ... \ EntityFramework.5.0.0 \ lib \ net45 \ EntityFramework.dll

I first divide the class into three classes (three separate * .cs files), and also use DataAnnotations in the download and download_category classes to use [key] for the id and problem columns.

my classes were lower in the 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; } } } 
0
source

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

0
source

Source: https://habr.com/ru/post/1369048/


All Articles