【发布时间】:2021-11-13 14:59:20
【问题描述】:
我正在使用 Entity Framework Core 开发一个 ASP.NET Core 5 项目。
我有这些实体
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”类型的导航“Student.TrainingMark”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。
请 - 有关如何解决此问题的任何帮助?
【问题讨论】:
-
您的学生实体只有一个导航属性
TrainingMarks。错误提到Student.TrainingMark。所以有些东西没有加起来。 -
您的另一个重复的question 刚刚关闭。以后不要多次打开同一个问题。
标签: c# entity-framework-core entity-framework-migrations