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

Query: FromSql not composing with LINQ when it contains certain whitespace patterns #7005

Closed
ra0o0f opened this issue Nov 12, 2016 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ra0o0f
Copy link

ra0o0f commented Nov 12, 2016

Hi

Following FromSql query does not compose with LINQ. No exception is raised and the result is correct BUT Sql Server Profiler shows that Where clause is not composing with FromSql query.

var result = dbContext.Permission
                .FromSql($@"
select
p.*
from Permission p
inner join
(
    select 
    r.RoleId 
    from 
    Role r
    where 
    Parents.exist('//I[.=sql:variable(""{{0}}"")]') = 1
    union all
    select {{0}} RoleId
) roles
on p.RoleId = roles.RoleId
", roleId)
                    .Where(c =>
                        c.Entity == entity &&
                        c.RoleId == roleId &&
                        c.Type == type &&
                        c.EntityId == entityId)
                        .ToList()

but the query in Sql Server Profiler is:

exec sp_executesql N'
select
p.*
from Permission p
inner join
(
    select 
    r.RoleId 
    from 
    Role r
    where 
    Parents.exist(''//I[.=sql:variable("@p0")]'') = 1
    union all
    select @p0 RoleId
) roles
on p.RoleId = roles.RoleId
',N'@p0 int',@p0=170

which shows Where clause is not included in query

EF Core version:1.0.1
Operating system: windows 10
Visual Studio version: vs 2015

@rowanmiller
Copy link
Contributor

Can you share a complete code listing (or project) that we can run to see the issue? We need to know what the various properties etc. in the model are to work out why this is being client evaluated.

@ra0o0f
Copy link
Author

ra0o0f commented Nov 16, 2016

@rowanmiller
complete code listing:

Entities:

    public class Role
    {
        public int RoleId { get; set; }

        public string Title { get; set; }

        public string Parents { get; set; }
    }

    public enum PermissionEntity
    {
        AttachmentCollection = 1,
        Category = 3,
    }

    public class Permission
    {
        public int PermissionId { get; set; }

        public PermissionEntity Entity { get; set; }

        public int RoleId { get; set; }

        public int EntityId { get; set; }
    }

DbContext:

    public class EfContext : DbContext
    {
        string connectionString;

        public EfContext(string connectionString) : base()
        {
            this.connectionString = connectionString;
        }

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

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Role>();
            modelBuilder.Entity<Permission>();
        }
    }

connection string

<add name="TestContext" connectionString="Data Source=.;Initial Catalog=EFTestDB;Integrated Security = true;" providerName="System.Data.SqlClient" />

LINQ query:

            string cnn = ConfigurationManager.ConnectionStrings["TestContext"].ConnectionString;

            int roleId = 77;


            using (EfContext context = new EfContext(cnn))
            {
                var result = context.Set<Permission>()
                .FromSql($@"
select
p.*
from Permission p
inner join
(
    select 
    r.RoleId 
    from 
    Role r
    where 
    Parents.exist('//I[.=sql:variable(""{{0}}"")]') = 1
    union all
    select {{0}} RoleId
) roles
on p.RoleId = roles.RoleId
", roleId)
                    .Where(c =>
                        c.Entity == PermissionEntity.AttachmentCollection &&
                        c.RoleId == roleId &&
                        c.EntityId == 22)
                        .ToList();
            }

Tables:

CREATE TABLE [dbo].[Permission](
    [PermissionId] [int] IDENTITY(1,1) NOT NULL,
    [Entity] [int] NOT NULL,
    [RoleId] [int] NOT NULL,
    [EntityId] [int] NOT NULL,
 CONSTRAINT [PK_Permission] PRIMARY KEY CLUSTERED 
(
    [PermissionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[Role](
    [RoleId] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](250) NOT NULL,
    [Parents] [xml] NULL,
 CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED 
(
    [RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

@divega divega added this to the 1.2.0 milestone Nov 18, 2016
@smitpatel
Copy link
Contributor

Query composition did not work because the Sql started with new line character and there is no space after Select. 😞

@smitpatel smitpatel added type-bug closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed type-investigation labels Nov 21, 2016
@ajcvickers ajcvickers changed the title FromSql not composing with LINQ Query: FromSql not composing with LINQ when it contains certain whitespace patterns May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants