Skip to content

Commit

Permalink
Fix Insert large collection data with BulkInsert
Browse files Browse the repository at this point in the history
  • Loading branch information
mbbipul committed Aug 25, 2020
1 parent d0296e0 commit cfce261
Show file tree
Hide file tree
Showing 145 changed files with 2,090 additions and 3,305 deletions.
5 changes: 4 additions & 1 deletion Controllers/OcrdatasetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ public async Task<ActionResult<OcrClass>> PostOcrClass(OcrClass ocrClass)
await repository.AddOcrClass(ocrClass);
return CreatedAtAction("GetOcrClass", new { id = ocrClass.Id }, ocrClass);
}
[DisableRequestSizeLimit]
[HttpPost("all")]
public async Task<ActionResult<OcrClass>> PostOcrClasses(List<OcrClass> ocrClasses)
{
Console.WriteLine(ocrClasses);
Console.WriteLine(ocrClasses.Count());
// This might speed up things a little aswell
await repository.AddOcrClasses(ocrClasses);
Console.WriteLine("done");
return CreatedAtAction("GetOcrClasses", new { count = ocrClasses.Count }, ocrClasses);
}
[HttpDelete("all")]
Expand Down
78 changes: 39 additions & 39 deletions Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace restApiDataset.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace ocrapi.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
52 changes: 52 additions & 0 deletions Migrations/20200825180015_InitialCreate.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Migrations/20200825180015_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace ocrapi.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "OcrClasses",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FileName = table.Column<string>(nullable: true),
ImageData = table.Column<string>(nullable: true),
GraphemeRootId = table.Column<string>(nullable: true),
VowelDiacreticId = table.Column<string>(nullable: true),
ConsonantDiacreticId = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_OcrClasses", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "OcrClasses");
}
}
}
50 changes: 50 additions & 0 deletions Migrations/OcrDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using restApiDataset.Models;

namespace ocrapi.Migrations
{
[DbContext(typeof(OcrDbContext))]
partial class OcrDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.7")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("restApiDataset.Models.OcrClass", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.Property<string>("ConsonantDiacreticId")
.HasColumnType("nvarchar(max)");

b.Property<string>("FileName")
.HasColumnType("nvarchar(max)");

b.Property<string>("GraphemeRootId")
.HasColumnType("nvarchar(max)");

b.Property<string>("ImageData")
.HasColumnType("nvarchar(max)");

b.Property<string>("VowelDiacreticId")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("OcrClasses");
});
#pragma warning restore 612, 618
}
}
}
3 changes: 2 additions & 1 deletion Models/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace restApiDataset.Models
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options){
this.Database.SetCommandTimeout(180);

this.Database.SetCommandTimeout(370);
}
public DbSet<OcrClass> OcrClasses { get; set;}
}
Expand Down
4 changes: 2 additions & 2 deletions Models/EFConsonantDiacretic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace restApiDataset.Models
{
public class EFConsonantDiacretic : IConsonantDiacretic
{
ApplicationDbContext context;
public EFConsonantDiacretic(ApplicationDbContext ctx){
OcrDbContext context;
public EFConsonantDiacretic(OcrDbContext ctx){
this.context = ctx;
}
public IQueryable<OcrClass> GetConsonantDiacretics(){
Expand Down
4 changes: 2 additions & 2 deletions Models/EFGraphemerootRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace restApiDataset.Models
{
public class EFGraphemerootRepository : IGraphemerootRepository
{
private ApplicationDbContext context;
public EFGraphemerootRepository(ApplicationDbContext ctx){
private OcrDbContext context;
public EFGraphemerootRepository(OcrDbContext ctx){
this.context = ctx;
}
public IQueryable<OcrClass> GetGraphemeroots(){
Expand Down
12 changes: 6 additions & 6 deletions Models/EFOcrclassRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace restApiDataset.Models
{
public class EFOcrclassRepository : IOcrclassRepository
{
private ApplicationDbContext context;
public EFOcrclassRepository(ApplicationDbContext ctx){
private OcrDbContext context;
public EFOcrclassRepository(OcrDbContext ctx){
this.context = ctx;
}
public IQueryable<OcrClass> OcrClasses => context.OcrClasses;
Expand All @@ -22,8 +22,8 @@ public async Task AddOcrClass(OcrClass ocrClass){
await context.SaveChangesAsync();
}
public async Task AddOcrClasses(List<OcrClass> ocrClass){
context.OcrClasses.AddRange(ocrClass);
await context.SaveChangesAsync();
await context.OcrClasses.BulkInsertAsync(ocrClass);
await context.BulkSaveChangesAsync();
}
public async Task<bool> UpdateOcrClass(OcrClass ocrClass){
OcrClass dbEntry = context.OcrClasses.
Expand Down Expand Up @@ -51,8 +51,8 @@ public async Task<OcrClass> DeleteOcrClassR(int ocrClassId){
return null;
}
public async Task DeleteAllOcrClass(){
context.OcrClasses.RemoveRange(context.OcrClasses);
await context.SaveChangesAsync();
await context.OcrClasses.BulkDeleteAsync(context.OcrClasses);
await context.BulkSaveChangesAsync();
}
public IQueryable<OcrClass> GetClassesByFontName(string fontName){
return context.OcrClasses
Expand Down
4 changes: 2 additions & 2 deletions Models/EFVowelDiacretic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace restApiDataset.Models
{
public class EFVowelDiacretic : IVowelDiacretic
{
private ApplicationDbContext context;
public EFVowelDiacretic(ApplicationDbContext ctx){
private OcrDbContext context;
public EFVowelDiacretic(OcrDbContext ctx){
this.context = ctx;
}
public IQueryable<OcrClass> GetVowelDiacretics(){
Expand Down
14 changes: 14 additions & 0 deletions Models/OcrDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;

namespace restApiDataset.Models
{
public class OcrDbContext : DbContext
{
public OcrDbContext(){}
public OcrDbContext(DbContextOptions<OcrDbContext> options) : base(options){
Database.SetCommandTimeout(150000);

}
public DbSet<OcrClass> OcrClasses { get; set;}
}
}
54 changes: 26 additions & 28 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace restApiDataset
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseDefaultServiceProvider(options =>
options.ValidateScopes = false);
});
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ocrapi
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Loading

0 comments on commit cfce261

Please sign in to comment.