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.

Scaffold-DbContext filter with schemas and tables mixed not working

See original GitHub issue

I have a huge Oracle 11 db with a lot of schemas/tables. When I run Scaffold-DbContext passing -Schemas and -Tables parameters all tables in all schemas are generated; -Tables filter is ignored.

I made a test installation of Oracle 11 using official docker image and I created an empty DB with sample schemas and tables:

CREATE USER SCHEMAA IDENTIFIED BY SCHEMAAPASSWORD;
/
GRANT CONNECT TO SCHEMAA;
/
GRANT CONNECT, RESOURCE, DBA TO SCHEMAA;
/
GRANT CREATE SESSION TO SCHEMAA;
/
CREATE USER SCHEMAB IDENTIFIED BY SCHEMABPASSWORD;
/
GRANT CONNECT TO SCHEMAB;
/
GRANT CONNECT, RESOURCE, DBA TO SCHEMAB;
/
GRANT CREATE SESSION TO SCHEMAB;
/
CREATE TABLE SCHEMAA.CUSTOMERS (
    ID NUMBER(10,0) NOT NULL,
    NAME VARCHAR2(250) NOT NULL,
    LASTNAME VARCHAR2(250) NOT NULL,
    PRIMARY KEY (ID)
)
/
CREATE TABLE SCHEMAB.EMPLOYEES (
    ID NUMBER(10,0) NOT NULL,
    NAME VARCHAR2(250) NOT NULL,
    LASTNAME VARCHAR2(250) NOT NULL,
    PRIMARY KEY (ID)
)
/
CREATE TABLE SCHEMAB.SALARIES (
    ID NUMBER(10,0) NOT NULL PRIMARY KEY,
    PAYDATE DATE NOT NULL,
    AMOUNT NUMBER(14,6) NOT NULL,
    CURRENCY VARCHAR2(50) NOT NULL,
    EMPLOYEE_ID NUMBER(10,0) NOT NULL,
    FOREIGN KEY (EMPLOYEE_ID) REFERENCES SCHEMAB.EMPLOYEES(ID)
)
/

Then, I run the following Scaffold-DbContext command:

Scaffold-DbContext "DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=XE)));PASSWORD=<<REDACTED>>;PERSIST SECURITY INFO=True;USER ID=SYSTEM" Oracle.EntityFrameworkCore -Force -Schemas SCHEMAA, SCHEMAB -Tables CUSTOMERS,EMPLOYEES

The expected result is that only CUSTOMERS and EMPLOYEES table are generated but when the command finishes execution also SALARIES appears in my .NET project

Since provider is not open sourced, to understand what’s going wrong, I enabled sql tracing on db and probably I found the bugged query:

SELECT t.table_name name, t.owner schema  FROM all_tables t WHERE t.table_name <> '__EFMigrationsHistory'  AND (t.owner IN (:s0, :s1)
OR t.table_name IN (:twiths0, :twiths1) AND CONCAT(t.owner, CONCAT(N'.', t.table_name)) IN (:sdott0, :sdott1))

Replacing parameters with values passed from command line:

SELECT t.table_name name, t.owner schema  FROM all_tables t WHERE t.table_name <> '__EFMigrationsHistory'  AND (t.owner IN ('SCHEMAA', 'SCHEMAB')
OR t.table_name IN ('EMPLOYEES', 'CUSTOMERS') AND CONCAT(t.owner, CONCAT(N'.', t.table_name)) IN ('SCHEMAA.CUSTOMERS', 'SCHEMAB.EMPLOYEES'));
NAME SCHEMA
CUSTOMERS SCHEMAA
EMPLOYEES SCHEMAB
SALARIES SCHEMAB

I think to problem is OR condition that need to be switched to an AND:

SELECT t.table_name name, t.owner schema  FROM all_tables t WHERE t.table_name <> '__EFMigrationsHistory'  AND (t.owner IN ('SCHEMAA', 'SCHEMAB')
AND t.table_name IN ('EMPLOYEES', 'CUSTOMERS') AND CONCAT(t.owner, CONCAT(N'.', t.table_name)) IN ('SCHEMAA.CUSTOMERS', 'SCHEMAB.EMPLOYEES'));
NAME SCHEMA
EMPLOYEES SCHEMAB
CUSTOMERS SCHEMAA

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
alexkehcommented, Jun 25, 2020

@TomaszGrzmilas My team decided to release one more beta before production. It will fix most of the bugs that have been so far in the beta 1.

2reactions
alexkehcommented, Jul 15, 2020

The Oracle EF Core 3.1 beta 2 will support the ability to define the specific tables to generate from multiple schemas. The syntax to use looks like the following:

Scaffold-DbContext -Tables SCHEMA1.TABLEA, SCHEMA2.TABLEB, SCHEMA3.TABLEC

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scaffold-DbContext multiple Schemas with specific tables
I have a single database that has multiple schema's. The two schemas are Bob and Bill. Both Bob and Bill contain Table1 Table2...
Read more >
After 10+ years of EF/EF Core, I can no longer justify using it.
I make schema changes all my team can apply same changes with running migrations (not specific to EF tho, probably it's same as...
Read more >
Scaffolding Or Reverse Engineering - Database
Developers can use the –Schemas and –Tables parameters to specify which schemas and tables/views to scaffold for an Entity Framework Core model.
Read more >
Scaffolding (Reverse Engineering) - EF Core
Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema.
Read more >
Entity Framework Core Database-First Tutorial for .NET Core
This command scaffolds a DbContext and entity type classes for a specified database. This tutorial shows how to create a simple console application,...
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