【问题标题】:Is there a way to create DbSets in Linqpad?有没有办法在 Linqpad 中创建 DbSet?
【发布时间】:2020-08-18 18:10:17
【问题描述】:

我正在使用 Linqpad6 的程序中测试一些查询。这是我要测试的查询。

var context = this;
var parameters = new UserQueryParameters();

var query = context.Set<UserEntity>()
            .AsNoTracking()
            .Filter(new UserQueryFilter(), parameters)
            .GroupBy(x => x.Name, x => x)
            .Select(x => x.Key ?? string.Empty)
            .OrderBy(x => x)
            .Paginate(parameters)
            .ConfigureAwait(false);

当我尝试运行它时,我得到了错误

无法为“UserEntity”创建 DbSet,因为该类型不是 包含在上下文模型中。

那我试试

var context = this;
var parameters = new UserQueryParameters();
var query = context.Users
            .AsNoTracking()
            .Filter(new UserQueryFilter(), parameters)
            .GroupBy(x => x.Name, x => x)
            .Select(x => x.Key ?? string.Empty)
            .OrderBy(x => x)
            .Paginate(parameters)
            .ConfigureAwait(false);

但这会产生错误:

方法'QueryExtensions.Filter(IQueryable, IQueryFilter, TParams?)' 不能 从用法推断。尝试指定类型参数 明确的。`

这是我正在使用的上下文:

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.ApplyConfiguration(new UserEntityTypeConfiguration());
        }

格式化查询的正确方法是什么?

【问题讨论】:

  • 您可以分享您的上下文吗?
  • 我在帖子中添加了数据库上下文。我正在使用配置在数据库中制作表格。

标签: c# linq linqpad


【解决方案1】:

是的,您可以从 linqpad 添加Connection(左上角)并连接到您的dll

这是doc 向您展示如何操作。

当您使用 Linqpad 时,您实际上是在上下文对象中。只需输入:

this.

您可以访问您的 ObejctContext 上的属性。

(也:确保使用“C# 语句”)

【讨论】:

  • 对不起!我忘记在查询顶部添加两行。我已经放了 var context = this ,但我仍然遇到问题。我也在使用 C# 语句。
  • 有什么问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多