Created
October 24, 2023 15:21
-
-
Save adamstirtan/79c0794152d7b64da14731a3e191b922 to your computer and use it in GitHub Desktop.
Issues with EF Core context management
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
| 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