- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Bogus是生成测试数据的好工具;但是,在 .net 5.0 上将它用于 C# Entity Framework 时我有两个问题
对于多对多关系——如何生成中间表的数据?我当前的 CourseStudent 表目前是空的。
对于一对一关系——我尝试生成 100 组学生数据;但是,如何为 StudentAddresses 生成准确的 100 条记录?当前表只有64条记录。
using System.Collections.Generic;
namespace EFCore_CodeFirst.Model
{
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; } //many-to-many relationship
}
}
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
关于c# - 如何在 EF Core C# 中使用 Bogus 为一对一和多对多关系生成测试数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66467861/
我在下面的代码中收到了一个单元化的警告,我很难找出原因。我看不到未初始化使用的代码路径。有人可以帮忙吗?此外,如果我的 goto 使用不当或者是否有更简洁的方法,我可以使用一些建议。 In func
我正在使用 Bogus生成测试数据。 有没有办法设置它使用的种子,以便它以相同的顺序生成相同的测试数据? 例如,这个测试会失败: var person1 = new Bogus.Person(); v
我正在尝试使用 Bogus 库在 .Net Core 2.1 应用程序中生成随机种子数据,使用 EF Core 进行数据管理。 我有一个名为 Company 的对象,它拥有一个地址;这是一对一的关系。
我正在使用 javap 研究为我的一个类(class)生成的代码并注意到以下输出: ... frame_type = 255 /* full_frame */ offset_delta = 11
我一直在摆弄 python re modules .search 方法。 cur 是来自 Tkinter 条目小部件的输入。每当我在条目小部件中输入“\”时,它都会抛出此错误。我不确定错误是什么或如何
我想用 Bogus 创建假数据库来测试数据库性能。这是我的书示例: public class Book { public Guid Id { get; set; }
我有一个使用 numpy.memmap 数组操作的 python3 脚本。它将数组写入位于 /tmp 中新生成的临时文件: import numpy, tempfile size = 2 ** 37
我的 head 标签中有以下内容: 问题是,第二行被认为是虚假评论,同一行的第二个标签被认为是评论的过早结束。 在同一行和第一个 endif 上有额外的标签只会给我两个虚假的评论错误。
我使用 Bogus用于生成测试数据的库。 例如我有一个类: public class Person { public int Id {get; set;} public List Phon
我正在尝试通过运行以下命令将 PHP 网站部署到 Google App Engine: appcfg.py -A update 我不断收到以下错误: Traceback (most recent
我知道这个问题被标记为“主观”,但我保证这是一个非常尖锐的问题并且与网络编程非常相关,并且将为将来可能遇到该线程的人提供很好的理解。 我找到了一个 Wiki,它详细说明了一个假设的程序,用于计算一个人
Can any one tell me why the Bogus chunk size error occurs. Exception at the swingToSvlt method:java.
Bogus是生成测试数据的好工具;但是,在 .net 5.0 上将它用于 C# Entity Framework 时我有两个问题 对于多对多关系——如何生成中间表的数据?我当前的 CourseStud
我从 Day 10 – Feed operators of the Perl 6 2010 Advent Calendar 拿了这个例子.uc略有变化为 .ucfirst那不再存在: my @raku
我有一个页面,其中包含多个TinyMCE(v4)编辑器,它们都可以很好地工作……直到我尝试添加: inline: true 他们的配置。当我这样做时,内联部分效果很好(工具栏消失了,然后在我聚焦编辑器
-[ display]: Ignoring bogus layer size (255211754908294243945860531626574872576.000000, 340282346638
我正在使用一个标准和简单的程序来读取一个 open_file 的所有数据包。open_file 意味着我正在使用 tcpdump 来捕获 eth0 接口(interface)上的所有流量并将其全部写入
好的,我从我的 JNI 代码中得到了一个伪造的方法描述符。我正在尝试获取我的类构造函数方法 ID,但它不是在玩球。我也试过, (void) V ,(V)V , (无效) 无效 这些都不行,但是,当我设
使用 Visual Studio 2010, MVC 3 (release version) 我在打开一个特定项目时收到此错误,该项目是我添加了 MVC 的传统 Web 表单项目: “Web 项目 '
转到 http://google.com/a/bogus/url 您将看到一个 404 页面(HTTP 代码为 404) 但是浏览器中的 url 仍然是 http://google.com/a/bog
我是一名优秀的程序员,十分优秀!