如何在EF Core C#中使用伪生成一对一和多对多关系的测试数据?
Bogus是生成测试数据的良好工具;但是,在.NET5.0上的C#实体框架中使用它时,我有两个问题如何在EF Core C#中使用伪生成一对一和多对多关系的测试数据?,c#,.net-core,entity-framework-core,bogus,C#,.net Core,Entity Framework Core,Bogus,Bogus是生成测试数据的良好工具;但是,在.NET5.0上的C#实体框架中使用它时,我有两个问题 对于多对多关系-如何为中间表生成数据?我现在的课程表现在是空的 对于一对一关系,我尝试生成100组学生数据;但是,如何为StudentAddress生成精确的100条记录?当前表只有64条记录 使用System.Collections.Generic; 命名空间EFCore\u CodeFirst.Model { 公营班级 { public int GradeId{get;set;} 公共
使用System.Collections.Generic;
命名空间EFCore\u CodeFirst.Model
{
公营班级
{
public int GradeId{get;set;}
公共字符串GradeName{get;set;}
公共字符串部分{get;set;}
公共虚拟ICollection学生{get;set;}
}
}
将代码更改为使用多对多联接表类,问题已解决
using System.Collections.Generic;
namespace EFCore_CodeFirst.Model.School
{
public class Course
{
//public Course()
//{
// this.Students = new HashSet<Student>();
//}
public int CourseId { get; set; }
public string CourseName { get; set; }
public int? TeacherId { get; set; }
public virtual Teacher Teacher { get; set; } //one-to-many relationship
//public virtual ICollection<Student> Students { get; set; } //many-to-many relationship (without join-table class)
public virtual ICollection<StudentCourse> StudentCourses { get; set; } //many-to-many relationship (WITH join-table class)
}
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasOne(s=>s.Address)
.带一个(ad=>ad.Student)
.HasForeignKey(ad=>ad.AddressOfStudentId);//一对一关系
//多对多关系
modelBuilder.Entity()
.HasKey(cs=>new{cs.CourseId,cs.StudentId});
modelBuilder.Entity()
.HasOne(cs=>cs.Student)
.有许多(s=>s.学生课程)
.HasForeignKey(s=>s.StudentId);
modelBuilder.Entity()
.HasOne(cs=>cs.Course)
.有许多(c=>c.学生课程)
.HasForeignKey(c=>c.CourseId);
SchoolDBInitializer schoolData=新SchoolDBInitializer();
modelBuilder.Entity().HasData(schoolData.Courses);
modelBuilder.Entity().HasData(schoolData.Grades);
modelBuilder.Entity().HasData(schoolData.Standards);
modelBuilder.Entity().HasData(schoolData.Students);
modelBuilder.Entity().HasData(schoolData.StudentAddresses);
modelBuilder.Entity().HasData(schoolData.StudentCourses);
modelBuilder.Entity().HasData(schoolData.Teachers);
}
Faker studentCourseFaker=new Faker()
.StrictMode(错误)
.UseSeed(5354)//设置特定的种子以生成每个伪造者的不同结果是一个好主意
.RuleFor(s=>s.StudentId,f=>f.PickRandom(Students).StudentId)//查找年级中的现有值
.RuleFor(s=>s.CourseId,f=>f.PickRandom(Courses.CourseId);
StudentCourses=studentCourseFaker.Generate(numToSeed)
.GroupBy(c=>new{c.StudentId,c.CourseId})。选择(c=>c.FirstOrDefault()).ToList()//删除重复项
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace EFCore_CodeFirst.Model
{
public class Student
{
public int StudentId { get; set; }
//The RowVersion is used as concurrency token, ensuring that you get an exception if a row you are updating has changed since you queried it.
[ConcurrencyCheck]
public string StudentName { get; set; }
public DateTime? DateOfBirth { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; }
//fully defined relationship
public int? GradeId { get; set; }
public virtual Grade Grade { get; set; } //one-to-many relationship
[Timestamp] //timestamp/rowversion - auto-generated by db when inserted/updated.
public byte[] RowVersion { get; set; }
public virtual ICollection<Course> Courses { get; set; } //many-to-many relationship
public virtual StudentAddress Address { get; set; } //one-to-one relationship
}
}
using Bogus;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace EFCore_CodeFirst.Model
{
public class SchoolDBInitializer
{
public List<Grade> Grades { get; set; }
public List<Course> Courses { get; set; }
public List<Student> Students { get; set; }
public List<StudentAddress> StudentAddresses { get; set; }
public SchoolDBInitializer()
{
const int numToSeed = 100;
var gId = 1;
Grades = new List<Grade>()
{
new Grade(){ GradeId = gId++, GradeName = "Grade 7", Section = "A" },
new Grade(){ GradeId = gId++, GradeName = "Grade 7", Section = "B" },
new Grade(){ GradeId = gId++, GradeName = "Grade 7", Section = "C" },
new Grade(){ GradeId = gId++, GradeName = "Grade 8", Section = "A" },
new Grade(){ GradeId = gId++, GradeName = "Grade 8", Section = "B" },
new Grade(){ GradeId = gId++, GradeName = "Grade 8", Section = "C" },
new Grade(){ GradeId = gId++, GradeName = "Grade 9", Section = "A" },
new Grade(){ GradeId = gId++, GradeName = "Grade 9", Section = "B" },
new Grade(){ GradeId = gId++, GradeName = "Grade 9", Section = "C" },
new Grade(){ GradeId = gId++, GradeName = "Grade 10", Section = "A" },
new Grade(){ GradeId = gId++, GradeName = "Grade 10", Section = "B" },
new Grade(){ GradeId = gId++, GradeName = "Grade 10", Section = "C" },
new Grade(){ GradeId = gId++, GradeName = "Grade 11", Section = "A" },
new Grade(){ GradeId = gId++, GradeName = "Grade 11", Section = "B" },
new Grade(){ GradeId = gId++, GradeName = "Grade 11", Section = "C" },
new Grade(){ GradeId = gId++, GradeName = "Grade 12", Section = "A" },
new Grade(){ GradeId = gId++, GradeName = "Grade 12", Section = "B" },
new Grade(){ GradeId = gId++, GradeName = "Grade 12", Section = "C" }
};
var cId = 1;
Courses = new List<Course>()
{
new Course() { CourseId = cId++, CourseName = "Chinese" },
new Course() { CourseId = cId++, CourseName = "English" },
new Course() { CourseId = cId++, CourseName = "Math" },
new Course() { CourseId = cId++, CourseName = "Computing" },
new Course() { CourseId = cId++, CourseName = "Art" },
new Course() { CourseId = cId++, CourseName = "Physics" },
new Course() { CourseId = cId++, CourseName = "Chemistry" },
new Course() { CourseId = cId++, CourseName = "Biology" },
new Course() { CourseId = cId++, CourseName = "History" }
};
var sId = 1;
var startDate = DateTime.ParseExact("20010101", "yyyyMMdd", CultureInfo.InvariantCulture);
var endDate = DateTime.ParseExact("20091231", "yyyyMMdd", CultureInfo.InvariantCulture);
Faker<Student> studentFaker = new Faker<Student>()
.StrictMode(false)
.UseSeed(1122) //It's a good idea to set a specific seed to generate different result of each Faker
.RuleFor(s => s.StudentId, f => sId++)
.RuleFor(s => s.StudentName, f => f.Name.FirstName())
.RuleFor(s => s.DateOfBirth, f => f.Date.Between(startDate, endDate))
.RuleFor(s => s.Height, f => f.Random.Decimal(120.5m, 195.5m))
.RuleFor(s => s.Weight, f => f.Random.Float(40.5f, 90.5f))
.RuleFor(s => s.GradeId, f => f.PickRandom(Grades).GradeId) //lookup existing value in Grades
//.RuleFor(s => s.CourseId, f => f.PickRandom(Courses).CourseId)
;
Students = studentFaker.Generate(numToSeed);
var saId = 1;
Faker<StudentAddress> studentAddressFaker = new Faker<StudentAddress>()
.StrictMode(false)
.UseSeed(1122) //It's a good idea to set a specific seed to generate different result of each Faker
.RuleFor(s => s.StudentAddressId, f => saId++)
.RuleFor(s => s.Address1, f => f.Address.StreetAddress())
.RuleFor(s => s.Address2, f => f.Address.SecondaryAddress())
.RuleFor(s => s.City, f => f.Address.City())
.RuleFor(s => s.Zipcode, f => f.Address.ZipCode())
.RuleFor(s => s.State, f => f.Address.State())
.RuleFor(s => s.Country, f => f.Address.Country())
.RuleFor(s => s.AddressOfStudentId, f => f.PickRandom(Students).StudentId);
StudentAddresses = studentAddressFaker.Generate(numToSeed);
}
}
}
using Microsoft.EntityFrameworkCore;
namespace EFCore_CodeFirst.Model
{
public class SchoolContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=SchoolDB;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasOne<StudentAddress>(s => s.Address)
.WithOne(ad => ad.Student)
.HasForeignKey<StudentAddress>(ad => ad.AddressOfStudentId); //one-to-one relationship
SchoolDBInitializer schoolData = new SchoolDBInitializer();
modelBuilder.Entity<Course>().HasData(schoolData.Courses);
modelBuilder.Entity<Grade>().HasData(schoolData.Grades);
modelBuilder.Entity<Student>().HasData(schoolData.Students);
modelBuilder.Entity<StudentAddress>().HasData(schoolData.StudentAddresses);
}
public DbSet<Student> Students { get; set; }
public DbSet<Grade> Grades { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<StudentAddress> StudentAddresses { get; set; }
}
}
namespace EFCore_CodeFirst.Model
{
public class StudentAddress
{
public int StudentAddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
//one-to-one relationship
public int AddressOfStudentId { get; set; }
public virtual Student Student { get; set; }
}
}
using System.Collections.Generic;
namespace EFCore_CodeFirst.Model
{
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
}
using System.Collections.Generic;
namespace EFCore_CodeFirst.Model.School
{
public class Course
{
//public Course()
//{
// this.Students = new HashSet<Student>();
//}
public int CourseId { get; set; }
public string CourseName { get; set; }
public int? TeacherId { get; set; }
public virtual Teacher Teacher { get; set; } //one-to-many relationship
//public virtual ICollection<Student> Students { get; set; } //many-to-many relationship (without join-table class)
public virtual ICollection<StudentCourse> StudentCourses { get; set; } //many-to-many relationship (WITH join-table class)
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace EFCore_CodeFirst.Model.School
{
public class Student
{
public int StudentId { get; set; }
//The RowVersion is used as concurrency token, ensuring that you get an exception if a row you are updating has changed since you queried it.
[ConcurrencyCheck]
public string StudentName { get; set; }
public DateTime? DateOfBirth { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; }
//fully defined relationship
public int? GradeId { get; set; }
public virtual Grade Grade { get; set; } //one-to-many relationship
public int? StandardId { get; set; }
public virtual Standard Standard { get; set; } //one-to-many relationship
[Timestamp] //timestamp/rowversion - auto-generated by db when inserted/updated.
public byte[] RowVersion { get; set; }
public virtual StudentAddress Address { get; set; } //one-to-one relationship
//public virtual ICollection<Course> Courses { get; set; } //many-to-many relationship (without join-table class)
public virtual ICollection<StudentCourse> StudentCourses { get; set; } //many-to-many relationship (WITH join-table class)
}
}
namespace EFCore_CodeFirst.Model.School
{
// Optional for EF Core 5.0
// without this class, the system generate
// Table name: CourseStudent
// Column names: CoursesCourseId, StudentsStudentId
//
// this class defines the column names of the join table (aka. bridging, junction or linking table)
// this provides a reference class for pre-loading the fake test data
// Typically, the join table contains just the entity key values of each side of the relationship.
//
//
// The relationship also needs to be configured via the Fluent API in OnModelCreating() of DbContext
public class StudentCourse
{
public StudentCourse(){}
public StudentCourse(Student student, Course course)
{
StudentId = student.StudentId;
Student = student;
CourseId = course.CourseId;
Course = course;
}
public int StudentId { get; set; } // System generated name without this class: StudentsStudentId
public Student Student { get; set; }
public int CourseId { get; set; } // System generated name without this class: CoursesCourseId
public Course Course { get; set; }
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasOne<StudentAddress>(s => s.Address)
.WithOne(ad => ad.Student)
.HasForeignKey<StudentAddress>(ad => ad.AddressOfStudentId); //one-to-one relationship
// many-to-many relationship
modelBuilder.Entity<StudentCourse>()
.HasKey(cs => new { cs.CourseId, cs.StudentId });
modelBuilder.Entity<StudentCourse>()
.HasOne(cs => cs.Student)
.WithMany(s => s.StudentCourses)
.HasForeignKey(s => s.StudentId);
modelBuilder.Entity<StudentCourse>()
.HasOne(cs => cs.Course)
.WithMany(c => c.StudentCourses)
.HasForeignKey(c => c.CourseId);
SchoolDBInitializer schoolData = new SchoolDBInitializer();
modelBuilder.Entity<Course>().HasData(schoolData.Courses);
modelBuilder.Entity<Grade>().HasData(schoolData.Grades);
modelBuilder.Entity<Standard>().HasData(schoolData.Standards);
modelBuilder.Entity<Student>().HasData(schoolData.Students);
modelBuilder.Entity<StudentAddress>().HasData(schoolData.StudentAddresses);
modelBuilder.Entity<StudentCourse>().HasData(schoolData.StudentCourses);
modelBuilder.Entity<Teacher>().HasData(schoolData.Teachers);
}
Faker<StudentCourse> studentCourseFaker = new Faker<StudentCourse>()
.StrictMode(false)
.UseSeed(5354) //It's a good idea to set a specific seed to generate different result of each Faker
.RuleFor(s => s.StudentId, f => f.PickRandom(Students).StudentId) //lookup existing value in Grades
.RuleFor(s => s.CourseId, f => f.PickRandom(Courses).CourseId);
StudentCourses = studentCourseFaker.Generate(numToSeed)
.GroupBy(c => new { c.StudentId, c.CourseId }).Select(c => c.FirstOrDefault()).ToList(); //remove duplicate