[英]Seeding huge amount of data (EF Core)
Here is the script which I use manually after the database creating to generate useless data for testing:这是我在创建数据库后手动使用的脚本,用于生成无用数据进行测试:
DECLARE @index BIGINT
SET @index = 0
SET IDENTITY_INSERT Persons ON
WHILE @index < 50000
BEGIN
INSERT INTO Persons
(Id, [Name], Code)
VALUES
(NEWID(), CONCAT('Person', @index), @index)
SET @index = @index + 1
END
How can I run it using EF core in the moment of the database initialization or somehow using data seeding methods?如何在数据库初始化时使用 EF 核心或以某种方式使用数据播种方法运行它? All the answers around are about small amounts of data, but in my case, I work with ~ 50000 records.周围的所有答案都是关于少量数据的,但就我而言,我使用约 50000 条记录。
If you are using EF Core 2.1 and higher, then making use of HasData method is an ideal way to add seed data.如果您使用的是 EF Core 2.1 及更高版本,则使用HasData方法是添加种子数据的理想方法。
We can call it using ModelBuilder object in 'OnModelCreating' method to add the data as part of code first migrations.The data then gets seeded the very first time when a database is scaffold or initialized and migrations are applied.我们可以在“OnModelCreating”方法中使用 ModelBuilder object 调用它,以将数据添加为代码优先迁移的一部分。然后,当数据库搭建或初始化并应用迁移时,数据第一次被播种。
You can also combine it with Bogus to generate fake data for entities.您还可以将它与Bogus结合起来为实体生成假数据。
Inline is a scenario for creating 50000 objects as stated in question.内联是一种用于创建 50000 个对象的方案,如问题所述。 The program executed successfully without any issues.该程序成功执行,没有任何问题。 In fact ef core is intelligent enough to split the data into batch queries of 700-800 objects per batch and push it to database.事实上,ef core 足够智能,可以将数据拆分为每批 700-800 个对象的批量查询并将其推送到数据库。
Entity:实体:
public class Person
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
}
OnModelCreating OnModelCreating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
int id = 1;
var fakePersons = new Faker<Person>().StrictMode(true)
.RuleFor(o => o.Id, f => id++)
.RuleFor(u => u.FirstName, (f, u) => f.Name.FirstName())
.RuleFor(u => u.LastName, (f, u) => f.Name.LastName());
var persons = fakePersons.Generate(50000);
modelBuilder.Entity<Person>().HasData(persons);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.