使用迁移插入大数据种子,代码优先实体框架核心

时间:2021-02-08 18:49:52

标签: c# asp.net-core .net-core entity-framework-core ef-code-first

我使用虚拟数据创建了迁移。对于虚拟数据,我使用 Bogus。通过“添加迁移”,我创建了迁移。之后我运行“Update-Database”,我得到了堆栈溢出异常。根据我的研究,我得出结论,这是我的大数据集(10000 条记录)中的问题。在 .NET Core 之前,我使用 Laravel,从大型数据集播种中我使用数组块。是否有可能在 Entity Framework Core 中也使用数组块或有其他解决方案?

来自更新数据库的错误:

<块引用>

堆栈溢出。 在 CRM_WEB_Service.Migrations.Coupon.Up(Microsoft.EntityFrameworkCore.Migrations.MigrationBuilder) 在 Microsoft.EntityFrameworkCore.Migrations.Migration.BuildOperations(System.Action`1) 在 Microsoft.EntityFrameworkCore.Migrations.Migration.get_UpOperations() 在 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Microsoft.EntityFrameworkCore.Migrations.Migration, Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerationOptions) 在 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator+<>c__DisplayClass16_2.b__2() 在 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(System.String) 在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(System.String, System.String, System.String) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(System.String, System.String, System.String) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor+UpdateDatabase+<>c__DisplayClass0_0.<.ctor>b__0() 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor+OperationBase.Execute(System.Action) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor+UpdateDatabase..ctor(Microsoft.EntityFrameworkCore.Design.OperationExecutor, Microsoft.EntityFrameworkCore.Design.IOOperationResultHandler, System.Collections.IDictionary) 在 System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean, Boolean) 在 System.Reflection.RuntimeConstructorInfo.Invoke(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo) 在 System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo) 在 System.Activator.CreateInstance(System.Type, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo, System.Object[]) 在 System.Activator.CreateInstance(System.Type, System.Object[]) 在 Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor.Execute(System.String, System.Object, System.Collections.IDictionary) 在 System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[[System.__Canon, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=5.0. 0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e],[System.__Canon,System.Private.CoreLib,Version=5.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e],[System.__Canon,System.Lib,Version=5.0.0.0。 5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Runtime.CompilerServices.CallSite, System.__Canon, System.__Canon, System.__Canon, System.__Canon) 在 Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.InvokeOperationImpl(System.String, System.Collections.IDictionary) 在 Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.InvokeOperation(System.String, System.Collections.IDictionary) 在 Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.UpdateDatabase(System.String, System.String, System.String) 在 Microsoft.EntityFrameworkCore.Tools.Commands.DatabaseUpdateCommand.Execute(System.String[]) 在 Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase+<>c__DisplayClass0_0.b__0(System.String[]) 在 Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(System.String[]) 在 Microsoft.EntityFrameworkCore.Tools.Program.Main(System.String[])

来自 DataContext 的代码:

        Randomizer.Seed = new Random(8675309);

        var ids = 1;
        var PromotionCode = new[] { "V27", "V25", "V13", "V21", "V22" };

        var coupon = new Faker<Coupon>()
            .RuleFor(c => c.Id, f => ids++)
            .RuleFor(c => c.GUID, f => f.Random.Guid())
            .RuleFor(c => c.VoucherPromotionCode, f => f.PickRandom(PromotionCode))
            .RuleFor(c => c.PromoCode, (f, c) => c.VoucherPromotionCode + f.Random.Replace("*-****-****"))
            .RuleFor(c => c.AvailableQuantity, f => f.Random.Number(0, 10))
            .RuleFor(c => c.UsageCount, f => f.Random.Number(0,10))
            .RuleFor(c => c.MaxUsageCount, (f, c) => c.UsageCount + c.AvailableQuantity)
            .RuleFor(c => c.Ammount, f => ReturnCleanNumber(f.Random.Double(100, 400)))
            .RuleFor(c => c.ValidFrom, f => f.Date.Between(Convert.ToDateTime("2020-12-01 00:00:00"), Convert.ToDateTime("2021-01-01 00:00:00")))
            .RuleFor(c => c.ValidTo, f => f.Date.Between(Convert.ToDateTime("2021-01-01 00:00:00"), Convert.ToDateTime("2021-05-30 00:00:00")))
            .RuleFor(c => c.ExpiryDate, (f, c) => c.ValidTo)
            .RuleFor(c => c.MinimumValueCurrency, (f, c) => Convert.ToDouble(ReturnCleanNumber(f.Random.Double(100, 300))) + c.Ammount)
            .RuleFor(c => c.CouponTypeId, f => f.Random.Number(1,2))
            .RuleFor(c => c.CouponStatusId, (f,c) => GetCouponStatus(c.UsageCount, c.ExpiryDate))
            .RuleFor(c => c.CreatedDate, f => f.Date.Between(Convert.ToDateTime("2020-12-01 00:00:00"), Convert.ToDateTime("2021-01-01 00:00:00")));

        for (int i = 0; i<100; i++)
        {
            var coupons = coupon.Generate(100);

            foreach(var c in coupons)
            {
                modelBuilder.Entity<Coupon>().HasData(c);
            }
            
        }

0 个答案:

没有答案