微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我使用虚拟数据创建了迁移。对于虚拟数据,我使用 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) 在 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.Globalization.CultureInfo) 在 System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags,System.Globalization.CultureInfo) 在 System.Activator.CreateInstance(System.Type,System.Reflection.BindingFlags,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,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,PublicKeyToken=7cec85d7bea7798e]](System.Runtime.CompilerServices.CallSite,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) 在 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,10))
            .RuleFor(c => c.MaxUsageCount,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,c) => c.ValidTo)
            .RuleFor(c => c.MinimumValueCurrency,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,c) => GetCouponStatus(c.UsageCount,c.ExpiryDate))
            .RuleFor(c => c.CreatedDate,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);
            }
            
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。