Created
November 20, 2018 05:14
-
-
Save rafaftahsin/d534245f2a64b25297488c39147be8ab to your computer and use it in GitHub Desktop.
SqliteDB to PostGRE Database seeding with C# ASP.NET Core 2.1 MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.DependencyInjection; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace SqliteToPostGre.Models | |
{ | |
public class SeedData | |
{ | |
public static void Initialize(IServiceProvider serviceProvider) | |
{ | |
IQueryable<Movie> Movies; | |
using (var sqliteContext = new SqliteMovieContext( | |
serviceProvider.GetRequiredService < DbContextOptions < SqliteMovieContext >>())) | |
{ | |
Movies = from m in sqliteContext.Movies select m; | |
} | |
using (var postgreContext = new PostgreMovieContext( | |
serviceProvider.GetRequiredService<DbContextOptions<PostgreMovieContext>>())) | |
{ | |
foreach (var m in Movies) | |
{ | |
postgreContext.Movies.Add(m); | |
postgreContext.SaveChanges(); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp2.1</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.AspNetCore.App" /> | |
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> | |
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4" /> | |
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4" /> | |
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.6" /> | |
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.2" /> | |
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="2.0.0-preview1" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.HttpsPolicy; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using SqliteToPostGre.Models; | |
using Microsoft.EntityFrameworkCore; | |
namespace SqliteToPostGre | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.Configure<CookiePolicyOptions>(options => | |
{ | |
// This lambda determines whether user consent for non-essential cookies is needed for a given request. | |
options.CheckConsentNeeded = context => true; | |
options.MinimumSameSitePolicy = SameSiteMode.None; | |
}); | |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); | |
services.AddDbContext<SqliteMovieContext>(options => options.UseSqlite("Data Source=Movies.db")); | |
services.AddDbContext<PostgreMovieContext>(options => | |
options.UseNpgsql("Host=localhost;Database=testdb;Username=postgres;Password=jjj")); | |
SeedData.Initialize(services.BuildServiceProvider()); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseStaticFiles(); | |
app.UseCookiePolicy(); | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment