Skip to content

Instantly share code, notes, and snippets.

@irontoby
Last active December 10, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irontoby/f78fce681cde158bf31e7da31a6872a7 to your computer and use it in GitHub Desktop.
Save irontoby/f78fce681cde158bf31e7da31a6872a7 to your computer and use it in GitHub Desktop.
EF Core .Include() Tests
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace EfCoreTests
{
public class IncludeTests
{
[Fact]
public async Task IncludeWorksAsync()
{
var coll = new ServiceCollection();
coll.AddDbContext<TestIncludeDbContext>(opt =>
{
opt.UseInMemoryDatabase(nameof(IncludeTests));
});
var services = coll.BuildServiceProvider();
const string orderName = "Order ABC";
const string customerName = "Fred";
var customer = new Customer
{
FirstName = customerName,
LastName = customerName,
};
var order = new Order
{
Name = orderName,
};
customer.Orders.Add(order);
int orderId;
int customerId;
using (var scope = services.CreateScope())
using (var dbContext = scope.ServiceProvider.GetRequiredService<TestIncludeDbContext>())
{
dbContext.Customers.Add(customer);
var count = await dbContext.SaveChangesAsync();
Assert.Equal(2, count);
orderId = order.Id;
customerId = customer.Id;
}
using (var scope = services.CreateScope())
using (var dbContext = scope.ServiceProvider.GetRequiredService<TestIncludeDbContext>())
{
order = await dbContext.Orders.FirstOrDefaultAsync(x => x.Id == orderId);
Assert.NotNull(order);
Assert.Equal(orderId, order!.Id);
Assert.Equal(orderName, order.Name);
Assert.Equal(customerId, order.CustomerId);
// we didn't .Include() Customer so it will be null
Assert.Null(order.Customer);
}
using (var scope = services.CreateScope())
using (var dbContext = scope.ServiceProvider.GetRequiredService<TestIncludeDbContext>())
{
order = await dbContext.Orders.Include(x => x.Customer).FirstOrDefaultAsync(x => x.Id == orderId);
Assert.NotNull(order);
Assert.Equal(orderId, order!.Id);
Assert.Equal(orderName, order.Name);
Assert.Equal(customerId, order.CustomerId);
// Customer was .Include()d so it will be populated
Assert.NotNull(order.Customer);
Assert.Equal(customerId, order.Customer!.Id);
Assert.Equal(customerName, customer.FirstName);
Assert.Equal(customerName, customer.LastName);
Assert.Equal(1, order.Customer.Orders.Count);
}
}
private class TestIncludeDbContext : DbContext
{
public TestIncludeDbContext(DbContextOptions<TestIncludeDbContext> options) : base(options)
{
}
public DbSet<Order> Orders => Set<Order>();
public DbSet<Customer> Customers => Set<Customer>();
}
private class Order
{
[Key]
public int Id { get; set; }
public string? Name { get; set; }
public int CustomerId { get; set; }
public Customer? Customer { get; set; }
}
private class Customer
{
[Key]
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public ICollection<Order> Orders { get; } = new HashSet<Order>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment