Skip to content

Instantly share code, notes, and snippets.

@adamstirtan
Created October 24, 2023 15:21
Show Gist options
  • Select an option

  • Save adamstirtan/79c0794152d7b64da14731a3e191b922 to your computer and use it in GitHub Desktop.

Select an option

Save adamstirtan/79c0794152d7b64da14731a3e191b922 to your computer and use it in GitHub Desktop.
Issues with EF Core context management
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class EmployeeDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var context = new EmployeeDbContext())
{
var employee = new Employee
{
FirstName = "John",
LastName = "Doe"
};
context.Employees.Add(employee);
context.SaveChanges();
}
using (var context = new EmployeeDbContext())
{
var employees = context.Employees.ToList();
foreach (var emp in employees)
{
Console.WriteLine(emp.FirstName + " " + emp.LastName);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment