locked
How the below code Works RRS feed

  • Question

  • User1979860870 posted

    Hi

      How the Bold line Code works

    Task Add(Employee employee);

    public async Task Add(Employee employee)  
            {  
                employee.Id = Guid.NewGuid().ToString();  
                db.Employees.Add(employee);  
                try  
                {  
                    await db.SaveChangesAsync();  
                }  
                catch  
                {  
                    throw;  
                }  
            }  

    IEmployeeRepository.cs
    using System.Collections.Generic;  
    using System.Threading.Tasks;  
      
    namespace MVCwithWebAPI.Models  
    {  
        public interface IEmployeeRepository  
        {  
            Task Add(Employee employee);  
            Task Update(Employee employee);  
            Task Delete(string id);  
            Task<Employee> GetEmployee(string id);  
            Task<IEnumerable<Employee>> GetEmployees();  
        }  
    }  
    
    public class EmployeeRepository : IEmployeeRepository  
        {  
            private readonly SqlDbContext db = new SqlDbContext();  
            public async Task Add(Employee employee)  
            {  
                employee.Id = Guid.NewGuid().ToString();  
                db.Employees.Add(employee);  
                try  
                {  
                    await db.SaveChangesAsync();  
                }  
                catch  
                {  
                    throw;  
                }  
            }  
    }

    Thanks

    Tuesday, February 9, 2021 6:48 AM

Answers

  • User2041008840 posted

    Employee is the model class use for to get input from your cshtml page employee is obj. So Task is used when you want the asynchronous function. 
    check it by applying the Breakpoint and then debugger it. you will find the employee contains the value that you put in  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 9, 2021 10:05 AM