Home > Net >  The entity type 'X' requires a primary key to be defined
The entity type 'X' requires a primary key to be defined

Time:10-07

Issue description

I am creating a sample application according to the tutorial. The error shown in the title is displayed at the time of the following command.

dotnet-aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries -sqlite

Error details

Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...
Generating a new DbContext class 'MvcMovieContext'
Attempting to compile the application in memory with the added DbContext.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Movie'
The entity type 'Movie' requires a primary key to be defined.
If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'.
For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943. StackTrace:

I tried this:

  • Add [Key] above "Id" and then scaffolding command

It is not resolved and the same error as the title is displayed.

If there is an "Id" in the first place, it will be automatically determined as a primary key, so why do I get this error:

The entity type'X' requires a primary key to be defined

Models/Movie.cs

using System;
using System.ComponentModel.DataAnnotations;

namespace MvcMovie.Models
{
    public class Movie
    {
     [Key]
        public int Id { get; set; }
        public string Title { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }

        [DataType(DataType.Date)]
        public DateTime ReleaseDate { get; set; }
    }
}

I also tried:

Call HasNoKey in OnModelCreating and then scaffolding command.

This also does not resolve the problem, and the same error as shown is still displayed.

HasNoKey should be called in OnModelCreating for the error content. Is it okay to call it as MvcMovieContext?

However, it is inconsistent that MvcMovieContext is a class created when scaffolding is successful.

Since "DbContext class to use" is also required when scaffolding from the right-click menu of the controller folder, I think that it is necessary to create MvcMovieContext first.

However, creating the MvcMovieContext first did not solve the problem.

MvcMovieContext

using Microsoft.EntityFrameworkCore;
using MvcMovie.Models;

namespace MvcMovie.Data
{
    public class MvcMovieContext : DbContext
    {
        public MvcMovieContext(DbContextOptions<MvcMovieContext> options)
            : base(options)
        {
        }

        public DbSet<Movie> Movie { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Movie().HasKey(m => new { m.Id });
        }
    }
}

Software versions used:

.NET SDK (global.json):
 Version:   5.0.401
 Commit:    4bef5f3dbf

Runtime environment:
 OS Name:     Mac OS X
 OS Version:  11.0
 OS Platform: Darwin
 RID:         osx.11.0-x64
 Base Path:   /usr/local/share/dotnet/sdk/5.0.401/

Host (useful for support):
  Version: 5.0.10
  Commit:  e1825b4928

.NET SDKs installed:
  3.1.412 [/usr/local/share/dotnet/sdk]
  3.1.413 [/usr/local/share/dotnet/sdk]
  5.0.400 [/usr/local/share/dotnet/sdk]
  5.0.401 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.18 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.19 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.10 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.18 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.19 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.10 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

CodePudding user response:

Have you tried specifying that it be self-assigned value?

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

CodePudding user response:

Solved by the following method in MvcMovie.Models:

public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Genre { get; set; }
    [Column(TypeName = "decimal(5, 2)")] //Add
    public decimal Price { get; set; }

    [DataType(DataType.Date)]
    public DateTime ReleaseDate { get; set; }
}
  • Related