-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
is HasComputedColumnSql bug or wrong way to use? #27814
Comments
@ByZhouhang you seem to be setting up a computed column, and then wanting to use that in queries - that should work just fine. Are you sure you actually created the computed column in the database, e.g. by running a migration? If you're still having difficulties, please post a minimal, runnable code sample that shows the error happening. |
|
I think that approach have something wrong. U are setting the property as ignored with annotations and later making it a computed column. The computed columns are database stored. You can create a where clause with the custom search over the FirstName + LastName expresion. |
if i use DBFirst ,i must config computed column in db,not in code, is it right? |
yes, i misanderstanding the use with computed column, before i thought computed column can use but not stored in the database |
I think you're looking for #10768 |
Hi @ajcvickers, My actual case is even more complicated:
|
@michiproep please open a new issue with a minimal, runnable code sample - it's impossible to know from the above exactly what's going on. I'd advise checking check the SQL being executed when you apply your migration. |
@michiproep Note that if you call |
@ajcvickers : Just to double check before I open a issue: So you want me to create one bug and one feature request for this? |
@michiproep If you're not using migrations, then the equivalent change would need to be made to the database in some other way. |
@ajcvickers : Ok, I see what you saying. Any plans on integrating this? I would actually give it a try and override some ExpressionVisitor if you give me hint where to start since this part is really advanced stuff ;-) |
@michiproep Isn't that what #10768 covers? |
@ajcvickers : Well, this points in the right direction but |
@michiproep Then please file a new issue describing in detail what you want the behavior you be. |
@michiproep #10768 would likely mean that you use SQL to define the "virtual" column (similar to how you do HasComputedColumnSql), and EF integrates that SQL into queries. The results are still materialized into your entity CLR type's property, just like a normal column.
How would you use the results for modifications, given that the column doesn't exist in the database? Re navigations, I assume you're talking about such a virtual SQL column which returns other entities - that may be possible, or not, depending on implementation details. |
Hi @roji, thanks for your response. What I'm trying to with EFCore:
I again paste my example from above:
Do you think I have a chance with ExpressionVisitors to achieve this? |
I see, you want to both be able to query the column via a custom SQL (decrypt) and also save results back via another custom SQL (encrypt). #10768 indeed only covers querying. You seem to be looking for #10861, which would allow server-side value converters, wrapping values in arbitrary SQL which transform them in both directions. You are very unlikely do be able to do this yourself with expression visitors. However, for encrypting columns this is probably not the best way; among other things, decrypting on the server (e.g. via a |
Well, I know the security issue... But that's the way it is... I don't worry too much about this since it's on an internal vlan anyways. Some addtional info on why I can't change the db: more than 1000 procedures and and about 100 applications use this table for all kind of things. We are measuring about 1k active queries per second on this table, also stateful packages/procedures which do not allow any ddl change without major downtime. Crazy stuff... Anyways, migrating old applications without this feature might be impossible then since way more tables use a similar approach... |
There's one more way this could possibly work: you can wrap updates to the table with a stored procedure (see docs); this means all columns of the table needs to be passed to the store procedure, whose implementation can call |
Yes... this seems doable... |
Ok, I actually managed to achive what I need by overriding the VisitColumn(ColumnExpression ce) |
@michiproep I'd be very wary of going down that route; doing this properly - without introducing subtle bugs - is probably going to be difficult and require good knowledge of the query pipeline (and may also involve access to internal APIs which change across releases). I'd advise on finding another way. |
Well, you're right but it saves me a lot of work for now. |
Include your code
Include verbose output
Include provider and version information
EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2022)
The text was updated successfully, but these errors were encountered: