Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 实体框架asp.net核心关系_Asp.net Core_Model_Relationship - Fatal编程技术网

Asp.net core 实体框架asp.net核心关系

Asp.net core 实体框架asp.net核心关系,asp.net-core,model,relationship,Asp.net Core,Model,Relationship,我有两种型号: 用户: 和类别: using System.Collections.Generic; namespace EDiary.Models { public class Class { public int Id { get; set; } public User Educator { get; set; } public string ClassName { get; set; } public ICo

我有两种型号: 用户:

和类别:

using System.Collections.Generic;

namespace EDiary.Models
{
    public class Class
    {
        public int Id { get; set; }
        public User Educator { get; set; }
        public string ClassName { get; set; }
        public ICollection<User> Users { get; set; }
    }
}
使用System.Collections.Generic;
命名空间EDiary.Models
{
公共课
{
公共int Id{get;set;}
公共用户教育器{get;set;}
公共字符串类名{get;set;}
公共ICollection用户{get;set;}
}
}
当我添加迁移时,我得到一个错误:无法确定“用户”类型的导航“Class.Educator”所表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.ignore”忽略此属性。为什么?

将ClassId添加到用户:

public class User
    {
        .....
  public int? ClassId {get; set;}
     [ForeignKey(nameof(ClassId))]
        [InverseProperty("Users")]
        public Class Class { get; set; }
    }
和教育家一起上课

   public class Class
    {
        public int Id { get; set; }
       public string ClassName { get; set; }

         [ForeignKey("Educator")]
       public int? EducatorId {get; set;}
       public User Educator { get; set; }
      
          [InverseProperty("Class")]
        public ICollection<User> Users { get; set; }
    }
公共类
{
公共int Id{get;set;}
公共字符串类名{get;set;}
[外籍教师(“教育家”)]
公共int?教育ID{get;set;}
公共用户教育器{get;set;}
[逆属性(“类”)]
公共ICollection用户{get;set;}
}

它不起作用。试试这个。公共整数?ClassId{get;set;}你有什么错误?我一直都有同样的错误。你是否从类中删除了公共用户教育家{get;set;}?没有,因为教育家是教育家,我需要ClassId来定义学生的类。
   public class Class
    {
        public int Id { get; set; }
       public string ClassName { get; set; }

         [ForeignKey("Educator")]
       public int? EducatorId {get; set;}
       public User Educator { get; set; }
      
          [InverseProperty("Class")]
        public ICollection<User> Users { get; set; }
    }