简体   繁体   English

播种大量数据(EF Core)

[英]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.

相关问题 在 EF Core 6 中播种身份数据 - Seeding identity data in EF core 6 EF Core 播种数据取决于数据库名称 - EF Core Seeding Data Depending on Database Name 添加带有种子数据的迁移时,EF Core 返回 Stackoverflow - EF Core returning Stackoverflow when adding migration with seeding data 使用 EF 播种数据 - Seeding data using EF EF Core Seeding 机制不插入数据 - EF Core Seeding mechanism doesn't insert data EF Core 迁移 - 播种数据时的 PK 违规 - EF Core Migration - PK Violation when seeding data 如果 EF Core 5 以多对多关系播种数据 - Seeding data in many-to-many relation if EF Core 5 Linux上的Dotnet核心1.1:在使用EF Core播种数据时,有什么特别的方式比其他方式更好? - Dotnet core 1.1 on linux: Any particular way thats better than others when seeding data with EF Core? EF Core 2.1中的动态数据种子迁移生成取决于DbContext bool-flag以及OnModelCreating()中的用法 - Dynamic data seeding migration-generation in EF Core 2.1 depending on DbContext bool-flag with usage in OnModelCreating() 在EF Core 3中播种相关数据,获取错误:对象引用未设置为对象的实例 - Seeding Related Data in EF Core 3, getting error: Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM