Reverse engineering - Different behaviour whether sql user is db_owner or not
See original GitHub issueReverse engineering with a SQL server user which is a db_owner produces code different from code generated with a non dbo user (i.e. db_datareader, db_datawriter) for not nullable bit columns with default values.
Steps to reproduce
Create a SQL Server table with a not nullable bit column.
CREATE TABLE [dbo].[TestBitColumn](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Hidden] [bit] NOT NULL
CONSTRAINT [PK_TestBitColumn] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TestBitColumn] ADD CONSTRAINT [DF_TestBitColumn_Hidden] DEFAULT ((1)) FOR [Hidden]
GO
Perform a reverse engineering with a SQL Server which has the db_owner role.
You’ll see the following code:
public partial class TestBitColumn
{
[Key]
[Column("ID")]
public int Id { get; set; }
[Required]
public bool? Hidden { get; set; }
}
Perform a reverse engineering with a SQL Server which doesn’t have the db_owner role.
You’ll see the following code:
public partial class TestBitColumn
{
[Key]
[Column("ID")]
public int Id { get; set; }
public bool Hidden { get; set; }
}
The same code will be generated for a not nullable bit column without a default value, no matter whether the user used for reverse engineering is dbo or not.
Expected behaviour
IMHO a not nullable bit column should never be mapped as a nullable bool property, no matter whether the column has a default value or not, the user is dbo or not, … For me, the following code is the correct one in each scenario.
public bool Hidden { get; set; }
Further technical details
EF Core Power Tools version: 2.4.217.0 (none of the preview options are checked in the settings dialog)
Database engine: SQL Server 2017
Visual Studio version: VS 2019, 16.7.4
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (7 by maintainers)
Top GitHub Comments
I was able to replicate, this is by design: https://docs.microsoft.com/en-us/sql/relational-databases/security/metadata-visibility-configuration?view=sql-server-ver15#benefits-and-limits-of-metadata-visibility-configuration
You can allow the restricted user to view default definition with:
GRANT VIEW DEFINITION to limited_user;
(In other words, db_owner or even db_datareader is not a requirement)
@smitpatel @bricelam FYI!
I added a check: https://github.com/ErikEJ/EFCorePowerTools/commit/2a7525a2bdd0b0d68daede74a4faec34d1f03cba - let’s see how that goes…
@smitpatel