Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToQueryString command can't execute query #2560

Closed
Cricle opened this issue Nov 16, 2022 · 2 comments
Closed

ToQueryString command can't execute query #2560

Cricle opened this issue Nov 16, 2022 · 2 comments

Comments

@Cricle
Copy link

Cricle commented Nov 16, 2022

Code here

using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Linq.Expressions;

internal class Program
{
    private static void Main(string[] args)
    {
        var opt = new DbContextOptionsBuilder()
            .UseNpgsql("Host=192.168.1.93;Port=5432;Database=6a997b6d6800421c9a0987ee25da881c_data;Username=postgres;Password=123456;");

        var dbc = new AppDbContext(opt.Options);
        var now = DateTime.Now;
        var raw = dbc.Students.Take(100);
        var query = raw.ToQueryString();
        var conn=dbc.Database.GetDbConnection();
            conn.Open();
        using (var command=conn.CreateCommand())
        {
            command.CommandText= query;
            using (var reader= command.ExecuteReader())//ERROR!
            {
                while (reader.Read())
                {

                }
            }
        }
        var datas = raw.ToList();//OK!
    }
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions options) : base(options) { }

        public DbSet<Student> Students => Set<Student>();
    }
    [Table("1743bc3e-826e-4419-b10a-590d48025de1")]
    public class Student
    {
        [Key]
        public string _id { get; set; }

        public DateTime? a4 { get; set; }
    }
}

Such sql will be generated

-- @__p_0='100'
SELECT "1"._id, "1".a4
FROM "1743bc3e-826e-4419-b10a-590d48025de1" AS "1"
LIMIT @__p_0

Execute in pgAdmin will got

ERROR:  column "__p_0" does not exist
LINE 4: LIMIT @__p_0
               ^
SQL state: 42703
Character: 98
@Cricle
Copy link
Author

Cricle commented Nov 16, 2022

Duplicate of #1232

@Cricle Cricle closed this as completed Nov 16, 2022
@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Nov 16, 2022
@roji
Copy link
Member

roji commented Nov 16, 2022

Duplicate of #1232

@roji roji marked this as a duplicate of #1232 Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants