Last active
August 29, 2019 18:52
-
-
Save panicoenlaxbox/379e1dc149c29df6273ef1e5fc2687de to your computer and use it in GitHub Desktop.
EF Core playground
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"> | |
// <PropertyGroup> | |
// <OutputType>Exe</OutputType> | |
// <TargetFramework>netcoreapp2.2</TargetFramework> | |
// <LangVersion>latest</LangVersion> | |
// </PropertyGroup> | |
// <ItemGroup> | |
// <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" /> | |
// <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" /> | |
// </ItemGroup> | |
// </Project> | |
using System; | |
using System.Threading.Tasks; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore.Design; | |
namespace SavingData | |
{ | |
public class ShopContext : DbContext | |
{ | |
public ShopContext(DbContextOptions options) : base(options) | |
{ | |
} | |
public DbSet<Order> Orders { get; set; } | |
} | |
public class Order | |
{ | |
public int Id { get; set; } | |
public DateTime OrderDate { get; set; } | |
} | |
class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ShopContext> | |
{ | |
public ShopContext CreateDbContext(string[] args) | |
{ | |
var options = new DbContextOptionsBuilder<ShopContext>() | |
.UseSqlServer(@"Server=(LocalDB)\MSSQLLocalDB;Database=Shop;Trusted_Connection=True;") | |
.Options; | |
return new ShopContext(options); | |
} | |
} | |
class Program | |
{ | |
private static async Task Main(string[] args) | |
{ | |
using (var context = CreateDbContext()) | |
{ | |
await context.Database.MigrateAsync(); | |
// TODO Do something... | |
} | |
} | |
public static ShopContext CreateDbContext() | |
{ | |
var options = new DbContextOptionsBuilder<ShopContext>() | |
.UseSqlServer(@"Server=(LocalDB)\MSSQLLocalDB;Database=Shop;Trusted_Connection=True;") | |
.Options; | |
return new ShopContext(options); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment