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

Free variables are no longer translated to bind variables if they are defined by the type #17557

Closed
cello99d opened this issue Sep 2, 2019 · 3 comments

Comments

@cello99d
Copy link

cello99d commented Sep 2, 2019

public class Blog
	{
		BloggingContext _ctx;
		public Blog(BloggingContext ctx) {
			_ctx = ctx;
		}

		public int BlogId { get; set; }
		public string Url { get; set; }
		public int Rating { get; set; }

		private Blog meField;
		private Blog This => this;
		private Blog MeFieldGet { get { return meField; } }
		private static string UrlGet() { return "Fo' and 1=1 and 'o"; }
		public int CountOthers() {
			var me = this;
			meField = this;
			int s1 = _ctx.Blogs.Where(b => b != this).Count();
			int s2 = _ctx.Blogs.Where(b => b != This).Count();
			int s3 = _ctx.Blogs.Where(b => b != me).Count();
			int s4 = _ctx.Blogs.Where(b => b != MeFieldGet).Count();
			int s5 = _ctx.Blogs.Where(b => b != meField).Count();
			return s1;
		}
	}

Using EF Core 2.2 the statements s2, s3, s4, s5 were translated to
SELECT COUNT(*) FROM "Blogs" AS "b"WHERE ("b"."BlogId" <> @__entity_equality_me_0_BlogId) OR @__entity_equality_me_0_BlogId IS NULL

Ef Core 3 translates s1, s2, s4, s5 to:
SELECT COUNT(*) FROM "Blogs" AS "b" WHERE "b"."BlogId" <> 99

Only acces to the local variable "me" in s3 ist still using a bind variable.
The edge case s1 was not translated as I would excpect in EF Core 2.2, see Query: Number is written into SQL Command Text instead of being a parameter when using 'this'

Using literals instead of bind variables causes performance problems in Oracle because it destroys the statement cache. I don't know about other brands but I guess its the same problem too.
Program.zip

@cello99d cello99d changed the title Free variables are no longer translated to bind variables if they defined by the type Free variables are no longer translated to bind variables if they are defined by the type Sep 2, 2019
@smitpatel
Copy link
Contributor

Duplicate of #17172

Generated SQL in latest daily builds (Entity Framework Core 3.0.0-rc1.19429.10)

SELECT COUNT(*)
FROM [Blogs] AS [b]
WHERE [b].[BlogId] <> 99

exec sp_executesql N'SELECT COUNT(*)
FROM [Blogs] AS [b]
WHERE ([b].[BlogId] <> @__entity_equality_This_0_BlogId) OR @__entity_equality_This_0_BlogId IS NULL',N'@__entity_equality_This_0_BlogId int',@__entity_equality_This_0_BlogId=99

exec sp_executesql N'SELECT COUNT(*)
FROM [Blogs] AS [b]
WHERE ([b].[BlogId] <> @__entity_equality_me_0_BlogId) OR @__entity_equality_me_0_BlogId IS NULL',N'@__entity_equality_me_0_BlogId int',@__entity_equality_me_0_BlogId=99

exec sp_executesql N'SELECT COUNT(*)
FROM [Blogs] AS [b]
WHERE ([b].[BlogId] <> @__entity_equality_MeFieldGet_0_BlogId) OR @__entity_equality_MeFieldGet_0_BlogId IS NULL',N'@__entity_equality_MeFieldGet_0_BlogId int',@__entity_equality_MeFieldGet_0_BlogId=99

exec sp_executesql N'SELECT COUNT(*)
FROM [Blogs] AS [b]
WHERE ([b].[BlogId] <> @__entity_equality_meField_0_BlogId) OR @__entity_equality_meField_0_BlogId IS NULL',N'@__entity_equality_meField_0_BlogId int',@__entity_equality_meField_0_BlogId=99
``

@cello99d
Copy link
Author

cello99d commented Sep 4, 2019

Is there any chance that query s1 could also be translated to using a bind variable?

@smitpatel
Copy link
Contributor

@cello99d - It is not easy to do that. Essentially we only see a constant in Expression tree. The only way we would make it parameter is to convert every constant ever appeared as a parameter. Which may not be desired behavior in all cases.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants