簡體   English   中英

實體類型“MigrationOperation”需要定義一個主鍵

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

我正在嘗試使用 Identity 構建我的第一個 .NET 應用程序。 這將是一個基本的鍛煉應用程序,但我一直無法弄清楚 Identity 中的鍵和外鍵。 這是我的運動控制器:

 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>();
    }

}`

但是,當我運行遷移時,出現錯誤

他們的實體類型“MigrationOperation”需要定義一個主鍵。

我遇到了其他實體的這個問題,在這種情況下,我只需要在我的 DbContext 類中創建一個新的 builder.Entity 。 然而,對於 MigrationOperation,情況似乎並非如此。 我是否完全錯誤地了解了如何將我的表與自動生成的 User 表連接起來? 謝謝

嘗試這個:

 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>(); }

為外鍵添加一個 int 並為主鍵定義一個 id 屬性

為了解決這個問題,最好通過你的 ID 定義私有的 id

private Guid _id;
[Key]
public Guid ID
{
get { return _id; }
}

暫無
暫無

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

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