如何修复实体框架核心中 X 类型的错误'Unable to determine the relationship represented by navigation X'



我正在做一个ASP。. NET Core 5项目使用实体框架Core.

有这些实体

public class StudentTrainingMark
{
public         int           Id              { get; set; }
public         decimal       TheoreticalMark { get; set; }
public         decimal       PracticalMark   { get; set; }
public         string        Note            { get; set; }
public         string        BranchId        { get; set; }
public         byte          FormationYearId { get; set; } 
public         string        StudentId       { get; set; }
public virtual Branch        Branch          { get; set; }
public virtual FormationYear FormationYear   { get; set; }
public virtual Student       Student         { get; set; }
}
public class Student
{ 
public string Id         { get; set; }
public string FirstName  { get; set; }
public string FamilyName { get; set; }
public virtual ICollection<StudentTrainingMark>   StudentTrainingMarks { get; set; }
}

我为每个实体创建了配置文件来做一些配置,如必需的字段和关系。

所以StudentTrainingMark也有这样的配置文件:

public class StudentTrainingMarkEntityConfig:IEntityTypeConfiguration<StudentTrainingMark>
{
public void Configure( EntityTypeBuilder<StudentTrainingMark> builder )
{
builder.HasKey( x => x.Id );
builder.Property( x => x.Id ).UseIdentityColumn();
builder.Property( x => x.TheoreticalMark ).IsRequired();
builder.Property( x => x.PracticalMark ).IsRequired();
builder.Property( x => x.BranchId ).IsRequired();
builder.Property( x => x.FormationYearId ).IsRequired();
builder.Property( x => x.StudentId ).IsRequired();
}
}

Student也有这样的配置文件:

public class StudentEntityConfig:IEntityTypeConfiguration<Student>
{
public void Configure( EntityTypeBuilder<Student> builder )
{
builder.HasKey( x => x.Id );
builder.Property( x => x.Id ).IsRequired();
builder.Property( x => x.FirstName ).IsRequired();
builder.Property( x => x.FamilyName ).IsRequired();
builder.Property( x => x.BirthDate ).IsRequired();
builder.Property( x => x.BirthPlace ).IsRequired();
builder.Property( x => x.Gender ).IsRequired();
builder.Property( x => x.Nationality ).IsRequired();
builder.Property( x => x.Address ).IsRequired();
builder.Property( x => x.DateOfRegistration ).IsRequired();
builder.Property( x => x.AcademicYearOfRegistration ).IsRequired();
builder.Property( x => x.StudentGuardianId ).IsRequired( false );
builder.Property( x => x.StudyLevelId ).IsRequired();
builder.Property( x => x.GroupId ).IsRequired();
}
}

到底是什么问题?

问题是当我尝试添加新的迁移时,我得到这个错误

无法确定导航"学生"所表示的关系。类型为"StudentTrainingMark"的TrainingMark。要么手动配置关系,要么使用'[NotMapped]'属性或'EntityTypeBuilder '忽略此属性。忽略'OnModelCreating'中的'。

请-任何帮助如何解决这个问题?

您可以设置它们的关系:

modelBuilder.Entity<StudentTrainingMark>()
.HasOne<Student>(s => s.Student)
.WithMany(g => g.StudentTrainingMarks)
.HasForeignKey(s => s.StudentId);

相关内容

  • 没有找到相关文章

最新更新