Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to solve this Error and why this happen ?

An unhandled exception occurred while processing the request.
ArgumentException: Entity type 'Employee' is defined with a 2-part composite key, but 1 values were passed to the 'DbSet.Find' method.
Microsoft.EntityFrameworkCore.Internal.EntityFinder<TEntity>.FindTracked(object[] keyValues, out IReadOnlyList<IProperty> keyProperties)

Stack Query Cookies Headers
ArgumentException: Entity type 'Employee' is defined with a 2-part composite key, but 1 values were passed to the 'DbSet.Find' method.
Microsoft.EntityFrameworkCore.Internal.EntityFinder<TEntity>.FindTracked(object[] keyValues, out IReadOnlyList<IProperty> keyProperties)
Microsoft.EntityFrameworkCore.Internal.EntityFinder<TEntity>.Find(object[] keyValues)
Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.Find(object[] keyValues)
TabDataAccess.Repositories.RepositoryTab<T>.GetById(object Id) in RepositoryTab.cs
+
            return dbSet.Find(Id);
WebTabCore.Controllers.EmployeeController.Create(Nullable<int> id) in EmployeeController.cs
+
                model = _repository.GetById(id);



repository interface

public interface IrepositoryTab<T> where T : class
        {
             T GetById(object Id);
        }


repository implementaion

public class RepositoryTab<T> : IrepositoryTab<T> where T : class
    {
        protected TabDbContext db { get; set; }
        private DbSet<T> dbSet;

        public RepositoryTab(TabDbContext Tabdb)
        {
            db = Tabdb;
            dbSet = db.Set<T>();
        }
        public T GetById(object Id)
        {
            return dbSet.Find(Id);
        }
        }

public class Employee
    {
  
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int EmployeeId { get; set; }
        public int BranchCode { get; set; }
        public string EmployeeName { get; set; }

    }

modelBuilder.Entity<Employee>()
          .HasKey(t => new { t.EmployeeId, t.BranchCode });


What I have tried:

What I have tried:

public class EmployeeController : Controller
   {

       private readonly IrepositoryTab<Employee> _repository;
       public EmployeeController(IrepositoryTab<Employee> emp)
       {
           this._repository = emp;
       }

       public async Task<IActionResult> Create(int? id)
       {
           var model = new Employee();
           if (id != null)
           {

               model = _repository.GetById(id);


           }
           
      }
       public IActionResult First()
        {
            var employee = _repository.GetAll().FirstOrDefault();
            return RedirectToAction("Create", new { id = employee.EmployeeId});
        } 

on view create 

  <button id="BtnFirst" onclick="location.href='@Url.Action("First", "Employee",new { id = Model.EmployeeId})'" style="display:inline">First</button>
Posted
Updated 27-Jan-19 5:21am
Comments
ahmed_sa 26-Jan-19 8:37am    
can any one help me please ?
I need to pass composit key to get by id
but also i have some tables have one key
so what i do please

1 solution

Find actually takes params[] so you can easily pass as many parts of your composite key as you need.
C#
public T GetById(int employeeId, int branchCode)
{
    return dbSet.Find(employeeId, branchCode);
}
 
Share this answer
 

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