asp.net-core - 获取错误 : Entity type 'Course' is defined with a single key property,,但 2 个值已传递给 'DbSet.Find' 方法

标签 asp.net-core asp.net-core-2.1 ef-core-2.1 entity-framework-core-2.1

我有一个GenericRepository,CourseRepository继承自它,Entities返回类(class)列表

 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>();
        }
    }

但是当我在 Razor 页面中运行此处理程序时

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

我收到以下错误:

实体类型“Course”是使用单个键属性定义的,但有 2 个值传递给“DbSet.Find”方法

这是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; }
}

这是类(class)配置

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);
    }
}

但是当我运行此代码时,我将不再收到此错误

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

这个错误的原因是什么?我的代码有错吗?我应该如何修复这个错误?

最佳答案

如果您检查这里,您使用的方法是错误的 FindAsync您可以看到,如果您想传递取消 token ,则需要将 key 作为数组传递,如下所示

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

关于asp.net-core - 获取错误 : Entity type 'Course' is defined with a single key property,,但 2 个值已传递给 'DbSet.Find' 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55758059/

相关文章:

c# - ASP.NET Core 2 从 HttpResponseMessage 获取 cookie

c# - 模型枚举字段的 ASP.NET 自定义错误消息

cookies - 如何禁用 ".AspNet.Consent"cookie 的自动创建?

c# - 将当前事务传递给 DbCommand

c# - 内存中的 EF Core SQLite 异常 : SQLite Error 1: 'near "MAX": syntax error'

c# - 如何在不使用 .Net Core 中的第三方记录器的情况下记录到文件?

unit-testing - 存储库模式和工作单元中的架构问题

c# - 如何在 ASP.Net Core 中验证上传的文件

asp.net-core - 如何在 F# Asp Core Web API 中序列化 F# 可区分的联合类型

c# - 如何在不使用 AutoMapper 的情况下手动映射 DTO?