C# 实体类型';迁移操作&x27;需要定义主键

C# 实体类型';迁移操作&x27;需要定义主键,c#,asp.net,.net-core,entity-framework-core,C#,Asp.net,.net Core,Entity Framework Core,我正在尝试使用Identity构建我的第一个.NET应用程序。这将是一个基本的锻炼应用程序,但我一直很难找出身份中的钥匙和外国钥匙。这是我的练习控制器: using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using WorkoutGenerator.Data.Migrations

我正在尝试使用Identity构建我的第一个.NET应用程序。这将是一个基本的锻炼应用程序,但我一直很难找出身份中的钥匙和外国钥匙。这是我的练习控制器:

 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
 using WorkoutGenerator.Data.Migrations;

    namespace RandomWorkout.Models
    {
    public class Exercise
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public MuscleGroup MuscleGroup { get; set; }
        public int MuscleGroupID { get; set; }
        [Key]
        public int ID { get; set; }

        [ForeignKey("ID")]
        public virtual ApplicationUser User { get; set; }

        public IList<ExerciseWorkout> ExerciseWorkouts { get; set; } = new List<ExerciseWorkout>();
    }

}`
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用WorkoutGenerator.Data.Migrations;
名称空间。模型
{
公开课练习
{
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共MuscleGroup MuscleGroup{get;set;}
public int MuscleGroupID{get;set;}
[关键]
公共int ID{get;set;}
[外国钥匙(“ID”)]
公共虚拟应用程序用户{get;set;}
public IList ExerciseWorkouts{get;set;}=new List();
}
}`
然而,当我运行迁移时,我得到了错误

实体类型“MigrationOperation”需要一个主键 定义


我在其他实体中遇到了这个问题,在这种情况下,我必须在DbContext类中创建一个新的builder.Entity。然而,对于MigrationOperation,情况似乎并非如此。在如何将我的表与自动生成的用户表联接方面,我是否完全走错了方向?感谢解决了这个问题,最好定义私有id,通过您的id获取

private Guid _id;
[Key]
public Guid ID
{
get { return _id; }
}
试试这个:

公开课练习{
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共MuscleGroup MuscleGroup{get;set;}
public int MuscleGroupID{get;set;}
[关键]
公共int Id{get;set;}
public int UserId{get;set;}
[外键(“用户ID”)]
公共虚拟应用程序用户{get;set;}
public IList ExerciseWorkouts{get;set;}=new List();
}

为外键添加一个int并为主键定义一个id属性

您是否尝试过使用
“id”
而不是
“id”
?@Marathon55刚刚尝试过。不幸的是,同样的结果。我觉得将我的表格加入到AspNetUsers表格非常简单,我只是不知道这会是一种多对多的关系吗?@Marathon55这是一种,是的。我还将实施一对多对另一的表格(训练)。我是否应该像我自己的桌子那样进行中间锻炼?(这是一张锻炼表)。我自己完成了这两个关系,我只是不知道如何使用这个身份库是的,没错,我得到了相同的迁移响应。操作实体需要一个主键。三个可能缩小范围的问题。1.我是否需要像处理个人多对多关系一样,在DbContext.cs中添加.Entity()HasKey.()?2.是否需要向ApplicationUser.cs文件添加任何代码?3.在包控制台中,添加迁移不是正确的命令吗(我需要从数据库中删除所有数据吗?),因此,我认为问题在于实体练习。请,您可以发布您的模型吗?哦?你知道可能是什么吗?我应该在ApplicationUser实体中浸泡一些东西吗?到目前为止它是完全空的,我不确定里面是否应该有任何东西。为此,我需要检查一下你的模型
public class Exercise {
    public string Name { get; set; } 
    public string Description { get; set; } 
    public MuscleGroup MuscleGroup { get; set; }
    public int MuscleGroupID { get; set; } 

    [Key] 
    public int Id { get; set; } 

    public int UserId {get; set;}
    [ForeignKey("UserId")] 
    public virtual ApplicationUser User { get; set; } 

    public IList<ExerciseWorkout> ExerciseWorkouts { get; set; } = new List<ExerciseWorkout>(); 
 }