Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Error : Entity type 'Course' is defined with a single key property, but 2 values were passed to the 'DbSet.Find' method

I have a GenericRepository,Which CourseRepository inherits from it, and Entities returns a list of courses

 public class CourseRepository : GenericRepositories<Course>, ICourseRepository
        {
            public CourseRepository(LearningSiteDbContext Context) : base(Context)
            {

            }
         }

 public class GenericRepositories<TEntity> : IGenericRepositories<TEntity> where TEntity : class, IEntity,new()
    {
        protected readonly LearningSiteDbContext context;
        public DbSet<TEntity> Entities { get; set; }

        public GenericRepositories(LearningSiteDbContext Context)
        {
            context = Context;
            Entities = context.Set<TEntity>();
        }
    }

But When I run this handler In Razor Page

 public async Task OnGetAsync(int Id, CancellationToken cancellationToken)
 {
    var selectedCourse = await courseRepository.Entities.FindAsync(Id,cancellationToken);
                Model = mapper.Map<CourseEditVm>(selectedCourse);
  }

I get the following error :

Entity type 'Course' is defined with a single key property, but 2 values were passed to the 'DbSet.Find' method

And this is Course entity class

public class Course 
{
    public Course()
    {

    }
    public Course(DateTime CreateDate)
    {
        this.CreateDate = CreateDate;
    }

    public int Id {get;set;}

    public string CourseTitle { get; set; }

    public string CourseDescription { get; set; }

    public decimal CoursePrice { get; set; }

    public string ImageName { get; set; }

    public string DemoFileName { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime? UpdateDate { get; set; }
    public bool IsDeleted { get; set; }

    //Foreign key
    public int? CourseStatusId { get; set; }
    public int? CourseLevelId { get; set; }
    public Guid? CustomUserId { get; set; }
    public int? CourseGroupId { get; set; }

    //Navigations
    public CourseStatus CourseStatus { get; set; }
    public CourseLevel CourseLevel { get; set; }
    public CustomUser CustomUser { get; set; }
    public CourseGroup CourseGroup { get; set; }
    public ICollection<CourseEpisod> CourseEpisods { get; set; }
    public ICollection<Keyword> Keywordkeys { get; set; }
}

And it's Course Config

class CourseConfig : IEntityTypeConfiguration<Course>
{
    public void Configure(EntityTypeBuilder<Course> builder)
    {
        builder.HasKey(c => c.Id);
        builder.Property(c => c.Id).ValueGeneratedOnAdd();
        builder.Property(c => c.CourseTitle).HasMaxLength(50);
        builder.Property(c => c.CourseDescription).HasMaxLength(400);
        builder.Property(c => c.ImageName).HasMaxLength(255);
        builder.Property(c => c.DemoFileName).HasMaxLength(255);

        //Relations
        builder.HasMany(c => c.Keywordkeys).WithOne(c => c.Course).HasForeignKey(c => c.CourseId);
        builder.HasOne(c => c.CustomUser).WithMany(c => c.Courses).HasForeignKey(c => c.CustomUserId);
    }
}

But when I run this code, I will no longer receive this error

  var selectedCourse = await courseRepository.Entities.FirstOrDefaultAsync(c => c.Id == Id, cancellationToken);

What is the cause of this error? Is my code wrong? How should I fix this error?

like image 525
Mehdi Sheykhveysi Avatar asked Apr 19 '19 07:04

Mehdi Sheykhveysi


1 Answers

You are using the wrong method, if you check here FindAsync you can see that if you want to pass a cancellation token, you need to pass your keys as an array like this

.FindAsync(new object[]{id}, cancellationToken);
like image 119
Hesam Kashefi Avatar answered Nov 19 '22 16:11

Hesam Kashefi