Home > Net >  I have a problem generating a controller with views in ASP.NET Core 6 MVC project using Visual Studi
I have a problem generating a controller with views in ASP.NET Core 6 MVC project using Visual Studi

Time:12-26

I'm using Visual Studio Professional 2022 (which license will end in 2 days) to create a website for my school project.

I'm using the code-first method. Therefore I have already created my model classes and my context page.

Here is the code of each class:

public class Game
{
    public int Id { get; set; }
    [Required]
    [MaxLength(255)]
    public string Name { get; set; }
    [Required]
    [MaxLength(255)]
    public string Genre { get; set; }
    [Required]
    [DataType(DataType.Url)]
    public string imageURL { get; set; }
    [Required]
    public float price { get; set; }
    [Range(1, 5), Required]
    public float rating { get; set; }
    [Required]
    public int MakerId { get; set; }
    public virtual Maker? Maker { get; set; }
}

public class Maker
{
    public int Id { get; set; }
    [Required, MaxLength(255)]
    public string Name { get; set; }
    [Required, MaxLength(255)]
    public string Nationality { get; set; }
    [Required, MaxLength(255)]
    public string Adresses { get; set; }
    [DataType(DataType.PhoneNumber)]
    public int Tel { get; set; }
    public virtual ICollection<Game>? Games { get; set; }
}

This is my DbContext:

public class GamesDbContext : DbContext
{
    public GamesDbContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<Game> Games { get; set; }
}

After all the above, I took the liberty to configure the connection string by taking it from a previous project that worked and changed only the database name to projectDB.

Once the migration and database update command finished, I checked whether the database was created successfully or not. And it has.

Finally, I wanted to create my controller and views by right clicking the controllers folder and choosing the "add controller" option. In which, I opted to create the controller with views using Entity Framework. Keep in mind that I have installed the following packages

  • Microsoft.EntityFrameworkCore.SqlServe(v6.0.12)
  • Microsoft.EntityFrameworkCore.Tools(v6.0.10)
  • Microsoft.EntityFrameworkCore.Design(v6.0.10)
  • Microsoft.VisualStudio.Web.CodeGeneration.Design(v6.0.10)

Here is the options screen :

Options

and here is the error :

Error

I have not faced this error before and would really appreciate it if someone could help me with this predicament.

PS: I already tried out the solutions in this Post

CodePudding user response:

When looking at the options screen it looks like you have chosen SR.Models.GamesModels for both model class and db context class. You should select your class Game for the model and GamesDbContext for the data context class in the options screen.

CodePudding user response:

It seems that your Microsoft.EntityFrameworkCore.SqlServe(v6.0.12) to be of a higher update than the others? You could reinstall this package to the same version as the others OR A tip would be if your Packages are acting up, make a Copy of your entire work. Then delete all your Dependency's reinstall them.

(just some extra) your DBset seems to be missing Maker try to use this in dbcontext

public DbSet<Maker> Makers { get; set; }
  • Related