using Microsoft.EntityFrameworkCore; using System.Collections.Generic; public class EmployeeContext : DbContext { public DbSetEmployees { get; set; } } public class Employee { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } } var optionsBuilder = new DbContextOptionsBuilder (); optionsBuilder.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=Employee;Trusted_Connection=True;"); using (var context = new EmployeeContext(optionsBuilder.Options)) { var employees = new List { new Employee { Name = "John Doe", Email = "[email protected]" }, new Employee { Name = "Jane Doe", Email = "[email protected]" } }; context.Employees.AddRange(employees); context.SaveChanges(); }
using Microsoft.AspNetCore.Mvc; [ApiController] [Route("[controller]")] public class EmployeeController : ControllerBase { [HttpGet("{id}")] public IActionResult GetEmployee(int id) { var employee = new Employee { Id = id, Name = "John Doe", Email = "[email protected]" }; return Ok(employee); } [HttpPost] public IActionResult AddEmployee(Employee employee) { // save employee to database return Ok(employee); } } public class Employee { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } }In this example, we create a controller class called EmployeeController that inherits from the ControllerBase class of the Microsoft.AspNetCore.Mvc package. We use attributes such as ApiController and Route to define the URL mapping for the controller actions. We define two controller actions called GetEmployee and AddEmployee. The GetEmployee action takes an integer parameter called id and returns an Employee object with the specified id. The AddEmployee action takes an Employee object in the request body and saves it to the database. Overall, C# provides a wide range of packages and libraries that can be utilized to develop robust and efficient business applications.