I have created a sample app (MvcMovie) according to the official tutorial and
The following command displays a heading error:
dotnet-aspnet-codegenerator controller-name MoviesController-m Movie-dc MvcMovieContext --relativeFolderPathControllers --useDefaultLayout --referenceScriptLibraries-sqlite
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 configure out the EntityFramework metadata for the model and DbContext: 'Movie'
The entity type 'Movie' requirements a primary key to be defined. If you extended 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:
Put [Key]
over "Id" and then the Scaffolding command
→ It does not resolve and displays the same error as the title
If there is an "Id" in the first place, it is automatically determined as the PrimaryKey, so
The entity type'X' requirements a primary key to be defined
There shouldn't be any errors?
·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;}
}
}
Call "HasNoKey" in OnModelCreating and then the Scaffolding Command → It does not resolve and displays the same error as the title
The error states that "OnModelCreating" should call "HasNoKey" but
Am I correct in understanding that the call is MvcMovieContext?
However, the contradiction is that "MvcMovieContext" is a class created upon successful scanning.
I believe that MvcMovieContext must be created before performing the scafolding, since the DBContext Class to be used is also required when performing scafolding from the right-click menu of the controller folder.
Also, the site I referred to made my own MvcMovieContext before the Scaffolding command.
Note: Tried the ASP.NET Core 5.0 MVC tutorial carefully ( (Add model and create DB)
他 Many other sites have created their own Context class.
However, creating "MvcMovieContext" earlier did not resolve the issue and
A heading error has been displayed.
·Data/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});
}
}
}
dotnet —info
.NET SDK (reflects global.json):
Version: 5.0.401
Commit—4bf 5f3dbf
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 runtime 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]
Solved by the following method.Thanks!
·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;}
}
}
© 2024 OneMinuteCode. All rights reserved.