gpt4 book ai didi

c# - C# 中 Migration 文件夹类内的意外操作

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:28 25 4
gpt4 key购买 nike

在编写初始模型后,我从控制台使用命令 Add-Migration 添加了迁移。创建了三个迁移文件,但我在文件中进行了一些意外和未定义的操作。

学生类(class):

public class Student

{
public string StudentsUserId { get; set; }
public bool is_accepted { get; set; }
public int Number_preferences { get; set; }

public List<Preference> Preferences { get; set; }
public List<Offers> Offers { get; set; }
public List List { get; set; }
[NotMapped]
public IList<Courscategory> courshistory { get; set; }

}

优惠类:

   public class Offers
{
public string CourseID { get; set; }

public string SupervisorUserId { get; set; }
public string Department { get; set; }
public string Email { get; set; }
public int Max_student { get; set; }
public string CourseName { get; set; }
public string CourseDescription { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public int CourseAvaialbility { get; set; }
public string Requirements { get; set; }
public bool IsFull { get; set; }


public Supervisors Supervisor { get; set; } //Supervisor is a separate class
public List<CList> List { get; set; }

}
}

列出类:

   public class List
{
public string CourseID { get; set; }
public string CourseName { get; set; }
public string StudentsUserId { get; set; }
public string SupervisorUserId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

public Offers Offer { get; set; }
public List<Students> Studnets { get; set; }
public List<Supervisors> Supervisor { get; set; }

public List<Preference> Preference { get; set; }

}
}

迁移文件中的代码:

migrationBuilder.CreateTable(
name: "CStudents",
columns: table => new
{
StudentsUserId= table.Column<string>(nullable: false),
is_accepted = table.Column<bool>(nullable: false),
Number_preferences = table.Column<int>(nullable: false),
ListCourseID = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_CStudents", x => x.StudentsUserId);
table.ForeignKey(
name: "FK_StudentsUserId_List_ListCourseID",
column: x => x.ListCourseID,
principalTable: "List",
principalColumn: "CourseID",
onDelete: ReferentialAction.Restrict);
});

初始模型代码:

public class CFE_CrazyLabContext : DbContext
{
public CFE_CrazyLabContext(DbContextOptions<CFE_CrazyLabContext> options)
: base(options)
{
}

public DbSet<LearnAngebot.Models.CStudents> Students{ get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<CStudents>().HasKey(o => o.StudentsUserId);

}
}

ListCourseId 在 Student 类中不存在,不是该类的变量。不知道这些操作是如何添加的。

最佳答案

编辑后,看起来你有点混淆了关系,根据你的评论,你说 Entity Framework 为你创建的外键是错误的,这是因为你交换了它。您添加到 StudentsUserId 中的 List 类的 FKey 目前只是一个额外的字段,没有什么特别之处。

在 student 类中,列表类的链接应该如下所示

public class Student

{
public string StudentsUserId { get; set; }
public bool is_accepted { get; set; }
public int Number_preferences { get; set; }

public List<Preference> Preferences { get; set; }
public List<Offers> Offers { get; set; }

public List<List> List { get; set; }

[NotMapped]
public IList<Courscategory> courshistory { get; set; }

}

在您的 List 类中,它应该如下所示:

public class List
{
public string CourseID { get; set; }
public string CourseName { get; set; }
public string StudentsUserId { get; set; }
public string SupervisorUserId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

public Offers Offer { get; set; }
public Student Student { get; set; }

public List<Supervisors> Supervisor { get; set; }
public List<Preference> Preference { get; set; }
}

生成的额外字段“ListCourseId”感觉就像是 EF 按照 [Table]+[PKey] 模式自动生成的字段。

关于c# - C# 中 Migration 文件夹类内的意外操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56773917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com