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