question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reverse engineering - Different behaviour whether sql user is db_owner or not

See original GitHub issue

Reverse 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:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ErikEJcommented, Sep 29, 2020

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!

1reaction
ErikEJcommented, Sep 30, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding SQL Server fixed database roles
If a db_owner does this, the table is owned by db_owner and is NOT available to other users of the database. This is...
Read more >
Implication of owned schemas for a user having db_owner ...
Every schema must have an owner, which is a database principal (user or role) within the database. The significance of the schema owner...
Read more >
What is the difference between "db_owner" and "the user ...
No, db_owner and the owner of the database are not the same. dbo is a user and db_owner is a database role. Databases...
Read more >
Encryption user error - Microsoft Q&A
I was trying one last test of Always Encrypted and this time I got the error "The reverse engineering operation cannot continue because...
Read more >
Hacking SQL Server Stored Procedures – Part 1
SQL Server allows DBAs to set databases as “trustworthy”. In a nutshell that means the trusted databases can access external resources like ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found