-
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
dotnet ef dbcontext scaffold doesn't work with computed columns unless user is db_owner #22842
Comments
I think @ErikEJ did similar investigation for some other issue. If whatever permission user has is not enough for us to get required data then schema generated will be based on data we got from server. |
@smitpatel Yes, this was my findings: ErikEJ/EFCorePowerTools#515 (comment) |
@yanbu0 no need for db_owner or even db_datareader, just VIEW DEFINITION right is needed. |
@smitpatel @ErikEJ Understood its a permissions issue, I guess my Issue submission here is more related to the fact that a lower than required level of permissions does not result in a scaffolding error, it results in incorrect scaffolding which will cause errors in the application using the model at runtime. Seems like throwing an error when not having the view definition right would be the better scenario here. It took me forever to figure out what was going on. |
Not in all cases. If a user is scaffolding a database which does not have objects requiring view definition, should be able to get scaffolded code. There won't be any error for such user. If we start throwing an error then it is breaking a working case scenario. If view definition is not granted then we cannot see that if there is something hiding behind it to throw conditionally. |
Agree with @smitpatel - only an issue with the definition column in sys.default_constraints |
I'd probably take the stance that its better to require additional permissions than build the tool to allow bugs to hit production. In the scenario where a developer (who has full rights to his local db and can scaffold properly) is using some sort of pipeline to push to prod, where the pipeline scaffolding has least permissions, then someone adds a computed column, this will almost certainly pass all integration tests and hit production. Maybe a flag in the cli would be the best of both worlds? If you know you don't have anything that requires view definition you can override with a '--noVewDef' or something, but default is that it is required? Maybe something in the docs at least? Just suggesting this since I was chasing my tail for a while figuring this out and if we hadn't been doing quite a bit of manual testing this would have hit prod. |
this will almost certainly pass all integration tests and hit production Why is that? Having an integration test doing inserts does not seem unreasonable? Also seems a bit odd to change source code in the pipeline !? |
|
Trying to help make it better here guys, IMO leaving this to (maybe, hopefully) be caught by tests isn't the way to go, but its your product. Lord knows I've been using and liking it for a long time and I'm not going to quit just because of this, haha. Thanks. |
We will discuss about a flag in next team meeting. Just blocking scenario which would be working does not feel like right way to go. |
I'm pretty sure we investigated this back in the EF6 days. I don't think there is a way to check for the permissions without that check itself running into issues. So ideally we would know that SQL Server is silently not returned things due to permissions, but in reality we can't determine that. However, I don't remember the details around this. |
Maybe log (Information?) if no default constraints found, that there could be a potential problem? This seems to work as well: Log on a limited user "nwreader" to Northwind (public role only):
Returns 0 As admin run in Northwind:
As limited user in Northwind run:
returns 1 |
This also includes default constraint definitions. |
I'd like to add that we just ran into something very similar. In our case a dev ran the scaffolding with a login that had limited permissions. It ran, but didn't generate the usual warnings about bools with default store value of true and to consider using nullable CLR types. (Which is a nasty problem covered in dozens of issues already). In any case, he didn't notice tested his code, did his check in and broke all kinds of things. It created nullable CLR types in the models, defaulted strings to null instead of empty string. Turns out, when we inspected the generated code, it generated different code in the dbcontext. It couldn't read the default values, but didn't complain. It just spit out different code depending on which login we used. This is really NOT GOOD. I understand it can be difficult to determine if an error occurred in SQL Server or there just isn't any data, but I think this needs to be investigated and fixed. We're using EF (scaffolding) Tools v3.1.7. This cost a LOT of hours and money to track down. |
@MikeYeager I added a warning dialog to EF Core Power Tools to address this: https://github.com/ErikEJ/EFCorePowerTools/blob/master/src/GUI/EFCorePowerTools/Helpers/ReverseEngineerHelper.cs#L30 |
Thank you! I'll make sure we're using this and checking for it from now on. |
@ErikEJ Is this something you might be able to submit a PR for? Or, if not, at least give some guidance on doing this based on your experience with the Power Tools? |
Sure, happy to. Would it be the proposed check with a warning (or even error) suggested here: #22842 (comment) |
@ErikEJ Warning is fine. |
Maybe the title should be changed to something like: unless user has been granted 'VIEW DEFINITION' |
@ErikEJ There is an additional bug here as raised on SO (cross-posting for posterity in case that answer is deleted):
I have confirmed this is an issue on my local SQL 2019 developer instance, and that it is resolved by adding SELECT HAS_PERMS_BY_NAME(quotename(DB_NAME()), 'DATABASE', 'VIEW DEFINITION'); Should a separate issue be filed for this? |
You're way ahead of me as usual. Wouldn't it be a good fix to backport to EF 6 and 7 though? |
@IanKemp I doubt it reaches the high bar for backporting |
To reproduce:
Create a new DB called TestDB, add table Table_1, add column "test" make primary key, run
alter table Table_1 add CalcField as (test + 'test-o-rama')
then from ef core project run
Result, note
.HasComputedColumnSql
is thereNow run with a user that has only db_datareader and db_datawriter:
Everything still scaffolds, and there is no error, but the
.HasComputedColumn
is gone, which really messes things up when trying to write back to the database since EF now tries to overwrite that column resulting in errors.One would think that db_datareader would either be enough to scaffold correctly, or an error would throw. It would be vastly preferable to not have to give everyone db_owner if they need to scaffold the DB.
The text was updated successfully, but these errors were encountered: