实体类型“X”需要定义主键

Posted

技术标签:

【中文标题】实体类型“X”需要定义主键【英文标题】:The entity type 'X' requires a primary key to be defined 【发布时间】:2021-11-27 03:30:17 【问题描述】:

问题描述

我正在根据tutorial 创建一个示例应用程序。标题中显示的错误是在执行以下命令时显示的。

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是脚手架成功时创建的一个类是不一致的。

由于从controller文件夹右键菜单搭建脚手架的时候也需要“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

【问题讨论】:

【参考方案1】:

您是否尝试过将其指定为自分配值?

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

【讨论】:

你好,亚历山德拉。感谢您的评论!我现在试过了,但没有用。namespace MvcMovie.Models ``` public class Movie [Key] // add [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // add public int Id get;放; 公共字符串标题 获取;放; 公共字符串类型 获取;放; 公共小数价格 获取;放; [DataType(DataType.Date)] public DateTime ReleaseDate get;放; ``` 我也尝试了以下,但结果是一样的。・MvcMovieContext `protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity() .Property(b => b.Id) .ValueGeneratedOnAdd(); ` 还有其他我能想到的方法吗?【参考方案2】:

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; 

【讨论】:

以上是关于实体类型“X”需要定义主键的主要内容,如果未能解决你的问题,请参考以下文章

实体类型“ModeratedUser”需要定义主键

EF Core:实体类型“用户”需要定义主键

Entity Framework Core “实体类型‘XXX’需要定义一个主键。”

实体类型“IdentityUserLogin<string>”需要定义一个主键[重复]

ASP.NET Core Web API - 实体类型“IdentityUserRole<long>”需要定义主键

Hibernate实体类编写规则和主键策略