問題描述
我正在根據教程創建一個示例應用程式。標題中顯示的錯誤是在執行以下命令時顯示的。
dotnet-aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --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 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:
我試過這個:
- 添加
[Key]上面的“Id”然后腳手架命令
未解決,顯示與標題相同的錯誤。
如果首先有一個“Id”,它會被自動確定為主鍵,那么為什么我會收到這個錯誤:
物體型別“X”需要定義一個主鍵
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; }
}
}
我也試過:
通話HasNoKey中OnModelCreating,然后腳手架命令。
這也不能解決問題,并且仍然顯示與所示相同的錯誤。
HasNoKey應該呼叫OnModelCreating錯誤內容。可以稱之為MvcMovieContext嗎?
但是,MvcMovieContext腳手架成功時創建的類是不一致的。
由于DbContext從控制器檔案夾的右鍵選單創建腳手架時也需要“使用的類”,所以我認為有必要先創建MvcMovieContext。
但是,創建第MvcMovieContext一個并沒有解決問題。
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 });
}
}
}
使用的軟體版本:
.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
uj5u.com熱心網友回復:
您是否嘗試過將其指定為自分配值?
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
uj5u.com熱心網友回復:
通過以下方法解決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; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/321750.html
標籤:C# 实体框架 .net核心 asp.net-core-mvc
上一篇:錯誤“無法將JSON值轉換為System.String。路徑:$[1].Interests[1].Meta[9].Content|LineNumber:0|BytePositionInLine:100
