Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem
How to get model property of employee as EmployeeName,BranchCode From Result variable in controller ?
I using asp.net core 2.1 visual studio 2017 repository pattern Generic
but problem cannot display or show models in EmployeesController
if i write result variable it not show EmployeeName or BranchCode

C#
public class EmployeesController : Controller
    {
        private readonly IEmployees _context;
  

        public EmployeesController(IEmployees context)
        {
            _context = context;
       
        }

public IActionResult Create(int? nextID)
        {
            
                
                int NextVal = nextID.HasValue ? nextID.Value : 0;
                model.EmployeeId = NextVal;
                var result =  _context.GetAll().SingleOrDefaultAsync(m => m.EmployeeId == NextVal);
                model.EmployeeName = result.EmployeeName
                model.BranchCode = result.BranchCode;

                
            }
Employee Models 
 public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        public int BranchCode { get; set; }
        public string EmployeeName { get; set; }


What I have tried:

in service layer i have interface Layer IEmployees as below :

public  interface IEmployees : IRepository<Employee>
    {

    }
Irepository Interface 

 public interface IRepository<T>
    {
        T Get(int id);
        T GetById(object id);
        T GetById(object id,object BranchCode);
        IQueryable<T> GetAll();
        Task<ICollection<T>> GetAllAsyn();
        IQueryable<T> GetAllIncluding(params Expression<Func<T, object>>[] includeProperties);
        Task<T> GetAsync(int id);
    }

     And implementation of Irepository<t> is

       
 public class EFRepository<T> : IRepository<T> where T : class
    {
        protected TabDbContext _context { get; set; }
        public EFRepository(TabDbContext context)
        {
            _context = context;
        }
       
        public IQueryable<T> GetAll()
        {
            return _context.Set<T>();
        }

        public virtual async Task<ICollection<T>> GetAllAsyn()
        {

            return await _context.Set<T>().ToListAsync();
        }

        public virtual T Get(int id)
        {
            return _context.Set<T>().Find(id);
        }
        public virtual T GetById(object id)
        {
            return _context.Set<T>().Find(id); 
        }

     IN TabDbContext is my dbcontext as below : 

 public class TabDbContext : DbContext
    {
        public TabDbContext(DbContextOptions<TabDbContext> options)
: base(options)
        { }

        public DbSet<Employee> Employees { get; set; }
     
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Employee>()
               .HasKey(t => new { t.EmployeeId,t.BranchCode });
        }
Posted
Comments
[no name] 12-Jan-19 12:58pm    
Over-engineered. Layering a generic DAL over an ORM. That's why EF "sucks" (not).

And you're not even checking the results of your queries; which could be null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900