简体   繁体   English

获取错误:实体类型“课程”是使用单个键属性定义的,但 2 个值已传递给“DbSet.Find”方法

[英]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我有一个 GenericRepository,CourseRepository 继承自它,Entities 返回一个课程列表

 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但是当我在 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实体类型“Course”使用单个键属性定义,但 2 个值被传递给“DbSet.Find”方法

And this is Course entity class这是 Course 实体类

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?我应该如何解决这个错误?

您使用了错误的方法,如果您在此处查看FindAsync,您可以看到如果您想传递取消令牌,则需要将您的密钥作为这样的数组传递

.FindAsync(new object[]{id}, cancellationToken);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体类型 &#39;IdentityUserToken<string> &#39; 使用单个键属性定义,但 3 个值已传递给 &#39;DbSet.Find&#39; 方法 - Entity type 'IdentityUserToken<string>' is defined with a single key property, but 3 values were passed to the 'DbSet.Find' method DbSet.Find() 不适用于 Guid 属性 - DbSet.Find() doesn't work for Guid properties ArgumentException:调用 'DbSet 的 position 0 处的键值<news> .Find' 是 'string' 类型,与 int' 的属性类型不匹配</news> - ArgumentException: The key value at position 0 of the call to 'DbSet<News>.Find' was of type 'string', which does not match the property type of int' 对“DbSet<>.Find”调用的 position 0 处的键值为“string”类型,与“long?”的属性类型不匹配。 - The key value at position 0 of the call to 'DbSet<>.Find' was of type 'string', which does not match the property type of 'long?' 从类型中获取DbSet - Get DbSet from type 实体类型“程序”要求定义主键 - The entity type 'Program' requires a primary key to be defined 实体类型 List 需要定义一个主键 - The entity type List requires a primary key to be defined 实体类型“AspNetUserLogins”需要定义主键? - The entity type 'AspNetUserLogins' requires a primary key to be defined? 实体类型需要定义一个主键 - Entity Type requires a primary key to be defined 实体类型“ ModeratedUser”需要定义主键 - The entity type 'ModeratedUser' requires a primary key to be defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM