簡體   English   中英

實體類型“X”需要定義主鍵

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

問題描述

我正在根據教程創建示例應用程序。 標題中所示的錯誤是在執行以下命令時顯示的。

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:

我試過這個:

  • 在“Id”上方添加[Key] ,然后是腳手架命令

未解決,顯示與標題相同的錯誤。

如果最前面有“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; }
    }
}

我也試過:

OnModelCreating中調用HasNoKey ,然后調用腳手架命令。

這也沒有解決問題,並且仍然顯示與所示相同的錯誤。

錯誤內容應該在HasNoKey中調用OnModelCreating 可以將其稱為MvcMovieContext嗎?

但是不一致的是MvcMovieContext是腳手架成功時創建的一個class。

由於從controller文件夾的右鍵菜單搭建腳手架時也需要“ DbContext class to use”,所以我認為有必要先創建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

您是否嘗試過指定它是自賦值?

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

通過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; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM