Skip to content

Instantly share code, notes, and snippets.

@scottsauber
scottsauber / ApplicationDbContext.cs
Created September 11, 2017 20:46
EF Core 1.x Customizations
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<OrderItem> Orders { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netstandard20</TargetFrameworks>
<PackageVersion>1.0.0</PackageVersion>
<PackageId>Package Name</PackageId>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
</PropertyGroup>
<ItemGroup>
@scottsauber
scottsauber / Startup.cs
Created October 13, 2017 19:32
InMemory DB Startup
public class Startup
{
public IConfiguration Configuration { get; }
public IHostingEnvironment CurrentEnvironment { get; }
public Startup(IConfiguration configuration, IHostingEnvironment currentEnvironment)
{
Configuration = configuration;
CurrentEnvironment = currentEnvironment;
}
public class ApplicationUsersControllerGetApplicationUser
{
private readonly ApplicationDbContext _context;
private readonly HttpClient _client;
public ApplicationUsersControllerGetApplicationUser()
{
var builder = new WebHostBuilder()
.UseEnvironment("Testing")
.UseStartup<Startup>();
[Produces("application/json")]
[Route("api/ApplicationUsers")]
public class ApplicationUsersController : Controller
{
private readonly ApplicationDbContext _context;
public ApplicationUsersController(ApplicationDbContext context)
{
_context = context;
}
[Fact]
public async Task DoesReturnNotFound_GivenUserDoesNotExist()
{
// Act
var response = await _client.GetAsync($"/api/ApplicationUsers/abc"); // No users with ID abc
// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public async Task DoesReturnOk_GivenUserExists()
{
// Arrange
var user = new ApplicationUser
{
Id = "123",
Email = "test@test.com"
};
@scottsauber
scottsauber / ApplicationUsersController.cs
Last active October 16, 2017 15:52
Full TestServer and InMemory DB Test
public class ApplicationUsersControllerGetApplicationUser
{
private readonly ApplicationDbContext _context;
private readonly HttpClient _client;
public ApplicationUsersControllerGetApplicationUser()
{
var builder = new WebHostBuilder()
.UseEnvironment("Testing")
.UseStartup<Startup>();
public class ApplicationUsersControllerGetApplicationUser
{
private readonly ApplicationDbContext _context;
private readonly HttpClient _client;
public ApplicationUsersControllerGetApplicationUser()
{
var builder = new WebHostBuilder()
.UseEnvironment("Testing")
.UseStartup<Startup>();
@scottsauber
scottsauber / ApplicationDbContext.cs
Last active October 17, 2017 11:27
Customizing EF Core tables using EF Core 2.0+
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<OrderItem> OrderItems { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);