Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
.NET - A demo console app showing how EF Core interceptor affects commands
class Program
{
static async Task Main(string[] args)
{
AddStudents();
Console.WriteLine("---------------------------------------------");
Console.WriteLine("With Interceptor....");
await GetAllStudents(applyInterceptor: true);
Console.WriteLine("---------------------------------------------");
Console.WriteLine("Without Interceptor....");
await GetAllStudents(applyInterceptor: false);
Console.WriteLine("---------------------------------------------");
}
static void AddStudents()
{
using var context = BuildUniversityContext();
context.Add(
new Student
{
FirstName = "John",
LastName = "Doe",
Address = "4 Privet Drive",
});
context.Add(
new Student
{
FirstName = "Jane",
LastName = "Doe",
Address = "4 Privet Drive",
});
context.SaveChanges();
}
static async Task GetAllStudents(bool applyInterceptor)
{
using var context = BuildUniversityContext();
IList<Student> studentCollection = null;
if (applyInterceptor)
{
studentCollection = await context.Students.TagWith("Apply OrderBy DESC").ToListAsync();
}
else
{
studentCollection = await context.Students.ToListAsync();
}
Console.WriteLine("Printing List of Students");
foreach(var student in studentCollection)
{
Console.WriteLine(student);
}
}
static UniversityContext BuildUniversityContext()
{
var dbContextBuilder = new DbContextOptionsBuilder<UniversityContext>();
// HARDCODED - For this demo.
var connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=University; Integrated Security=True;";
dbContextBuilder.UseSqlServer(connectionString);
return new UniversityContext(dbContextBuilder.Options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment