Skip to content
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

Reverse Engineer: Table/Schema Exclusion #6182

Open
Tracked by #22948
rowanmiller opened this issue Jul 27, 2016 · 41 comments
Open
Tracked by #22948

Reverse Engineer: Table/Schema Exclusion #6182

rowanmiller opened this issue Jul 27, 2016 · 41 comments

Comments

@rowanmiller
Copy link
Contributor

Have the ability to specify tables/schemas to be excluded, useful for excluding AapNet* tables etc.

@lajones
Copy link
Contributor

lajones commented Jul 27, 2016

See also this comment: #5126 (comment)

@Starkie
Copy link

Starkie commented Oct 12, 2018

Are there any news on the implementation of this functionality?

I think it would be useful, for example, when a table causes an error in the generation of the scaffold. I had this happen in #13528. As a workaround, all tables had to be referenced manually using the --table flag, excluding the two that caused the exception.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 13, 2018

Maybe EF Core Power Tools can help you? (Persists the table selection)

@Starkie
Copy link

Starkie commented Oct 13, 2018

The extension is only available for VS2017, right? I mostly use VS Code and run commands with the CLI (Linux user). 😓
Thanks for the suggestion though.

@ajcvickers
Copy link
Contributor

While there is some value in doing this at the command line (i.e for cross-plat), that value is diminished by the Power Tools, which seem to be the preferred choice of most people doing reverse engineering. This combined with table/schema selection being possible anyway means that we've decided not to implement this.

However, we would not be against a well-written and robust community contribution to implement this.

@ajcvickers ajcvickers added closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. and removed propose-close labels Jan 24, 2019
@ajcvickers ajcvickers removed this from the Backlog milestone Jan 25, 2019
@Starkie
Copy link

Starkie commented Jan 26, 2019

After looking up the code, I think that with a bit of guidance, this could be an easy implementation. I'll give it a shot over the weekend.

@Starkie
Copy link

Starkie commented Jan 28, 2019

I have been working on it over the weekend, with the idea of adding a parameter --exclude-tables. This parameter "flows" alongside the values of --tables.

To propagate these values to the database providers, the IDatabaseModelFactory interface must be modified: the excludeTables parameter must be added to the Create methods. This will be a breaking change for every provider, and I'm a bit worried about that.

https://github.com/aspnet/EntityFrameworkCore/blob/45cbc40aee746e27d69d391dc6457ecfc008a1b4/src/EFCore.Relational/Scaffolding/IDatabaseModelFactory.cs#L37

Could it be implemented as an optional parameter, to avoid the breaking change? Or is it prefered to update the providers to support this feature?

If needed, a PR could be created to better review the change.

@ajcvickers
Copy link
Contributor

@bricelam @smitpatel Is breaking IDatabaseModelFactory the way to go here?

@bricelam
Copy link
Contributor

bricelam commented Jan 29, 2019

Yeah, probably. Should add excludeSchemas at the same time.

BUT, I still feel like it’s better to add a command/API to get a list of all the tables and implement exclusion at a higher level. (e.g. via a PowerShell/bash script) The Power Tools already work this way.

@ajcvickers
Copy link
Contributor

Re-opening so we can discuss @bricelam's comments in triage.

@ajcvickers ajcvickers reopened this Jan 29, 2019
@ErikEJ
Copy link
Contributor

ErikEJ commented Jan 29, 2019

Being able to get the table list would be great, in Power Tools I currently have to use a custom way per ADO.NET provider to get the table list.

@Starkie
Copy link

Starkie commented Jan 30, 2019

Could a list subcommand be added to the database command, that returns the list of tables (and/or schemas)? It could also have an option to return unformatted output, so it might be used as an API by the PowerTools.

The command woud be invoked internally before a scaffolding operation if the --exclude-tables or --exclude-schemas parameters were used.

@ajcvickers
Copy link
Contributor

@Starkie We discussed this in triage and it seems like a reasonable idea. The only caveat is if it is too slow, but if the command specifically gets just table/schema name, and not everything else associated with the table, then it's probably okay.

Also, see #14602 which is about allowing new command functionality without always breaking the interface.

@ajcvickers ajcvickers added this to the Backlog milestone Feb 4, 2019
@ErikEJ
Copy link
Contributor

ErikEJ commented May 5, 2019

@ajcvickers Is this still open for a reason? (closed-wont-fix) ?
Is this about adding a feature to get a list of all tables/schemas ?

@ajcvickers
Copy link
Contributor

@ErikEJ I believe it is about getting a list, yes. This could potentially be used by tooling to automate more things. I think somebody said that your tooling already does this, but maybe it makes sense to move the code you have into EF?
/cc @bricelam

@bricelam
Copy link
Contributor

bricelam commented May 9, 2019

We should probably file a new issue for adding a command (and API) to get a list of tables...

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 22, 2019

@bricelam I would like to have a look at this, but before I begin it raises a number of design questions.

@bricelam
Copy link
Contributor

If you draft a proposal, I can present it to the team in a design meeting.

@kuntalbose
Copy link

kuntalbose commented Dec 5, 2019

Hi
Any update ?are you add a option for exclude table?the option If we can write like bellow format its better ,
-- [option] table1,table2,table3....etc

Thankx

@ajcvickers
Copy link
Contributor

@ikuntalb This issue is in the Backlog milestone. This means that it is not going currently scheduled for the next release. We have not finished planning for 5.0, so this may change. However, keep in mind that there are many other high priority features with which it will be competing for resources.

@mm-ryo
Copy link

mm-ryo commented May 12, 2020

Hi,
How do I ignore views when i run scaffold?
In EF core 3 migration, it will generate table for view. thanks

@ErikEJ
Copy link
Contributor

ErikEJ commented May 12, 2020

@robinNode you can specify -table options to include only the tables you want. You can also use EF Core Power Tools, gives you a GUI to select tables/views, and persists your selection.

@florinciubotariu
Copy link

Hi.
Unfortunately I couldn't use EF Core Power Tools so I've decided to go on another route. I'll post here my solution, maybe it will help others with the same problem.

Since I'm using SQL Server, I've decorated SqlServerDatabaseModelFactory using Scrutor. Extend the functionality as you like:

public class CustomSqlServerDatabaseModelFactory : IDatabaseModelFactory
 {
   private IDatabaseModelFactory databaseModelFactory;

   private static readonly List<string> ExcludedTables = new List<string>
   {
     "__MigrationHistory",
     "Exceptions"
   };

   public CustomSqlServerDatabaseModelFactory(IDatabaseModelFactory databaseModelFactory)
   {
     this.databaseModelFactory = databaseModelFactory;
   }

   public DatabaseModel Create(string connectionString, DatabaseModelFactoryOptions options)
   {
     var databaseModel = databaseModelFactory.Create(connectionString, options);

     RemoveTables(databaseModel);

     return databaseModel;
   }

   public DatabaseModel Create(DbConnection connection, DatabaseModelFactoryOptions options)
   {
     var databaseModel = databaseModelFactory.Create(connection, options);

     RemoveTables(databaseModel);

     return databaseModel;
   }

   private static void RemoveTables(DatabaseModel databaseModel)
   {
     var tablesToBeRemoved = databaseModel.Tables.Where(x => ExcludedTables.Contains(x.Name)).ToList();

     foreach (var tableToRemove in tablesToBeRemoved)
     {
       databaseModel.Tables.Remove(tableToRemove);
     }
   }
 }

And the implementation of IDesignTimeServices:

public class CustomEFDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
      serviceCollection.Decorate<IDatabaseModelFactory, CustomSqlServerDatabaseModelFactory>();
    }
}

I've also created a blog post with more details which can be read here.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 7, 2020

@florinciubotariu why were you unable to use EF Core Power Tools?

@florinciubotariu
Copy link

florinciubotariu commented Oct 7, 2020

I had to implement a custom pluralizer (to be as similar as possible to EF 6) and I saw the EF Core Power Tools didn't pick up my implementation of IDesignTimeServices. If I recall correctly, I had problems with tables ending in Data and Media, even though I was using the EF 6 Pluralizer. Is this a known limitation?

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 7, 2020

@florinciubotariu You can opt in to use the Legacy pluralizer, should be 100% EF6 compatible.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 7, 2020

@florinciubotariu If it is not compatible with the EF6 pluralizer, let me know (via a repro) and I will get it fixed

@florinciubotariu
Copy link

florinciubotariu commented Oct 7, 2020

@ErikEJ

EF Core 5 preview - checked
Use tables and column names directly from the database - checked
EF 6 Pluralizer - checked

EF 6:

    public virtual DbSet<XItemMedia> XItemMedias { get; set; }
    public virtual DbSet<XTestDrive> XTestDrives { get; set; }
    public virtual DbSet<XCSS> XCSSes { get; set; }

EF Core Power Tools:

    public virtual DbSet<XItemMedium> XItemMedia { get; set; }
    public virtual DbSet<XTestDrife> XTestDrives { get; set; }
    public virtual DbSet<XCSS> XCSSes { get; set; }

last one is ok but when using:

EF Core 5 preview - checked
Pluralize and singularize generated object names - checked
EF 6 Pluralizer - checked

I get:

EF 6:

    public virtual DbSet<XCSS> XCSSes { get; set; }
    public virtual DbSet<XBundlePurchas> XBundlePurchases { get; set; }

EF Core Power Tools

     public virtual DbSet<XCss> XCsses { get; set; }
     public virtual DbSet<XBundlePurcha> XBundlePurchas { get; set; }

I can't use Pluralize and singularize generated object names because I have column names ending in ID in my tables.
I hope that next week I will come with a repro on EF Core Power Tools repository.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 7, 2020

In the latest daily, you can combine UseDatabaseNames and Pluralization with EF Core 5. Looking forward to your repro in the EF Power Tools repository

@jcummings2
Copy link

jcummings2 commented Apr 7, 2023

Hi Any update ?are you add a option for exclude table?the option If we can write like bellow format its better , -- [option] table1,table2,table3....etc

Thankx

Good question and after reading through all the comments I am still a bit confused on what this issue is trying to do. The title still indicates an exclude option but some of the discussion seems to go in different directions. Based on numerous other issues that have been closed as duplicates, I gather the point may still be adding an exclude option. In any case, I sure hope the exclude option is still the purpose.

@MikeMcWilliams
Copy link

Is this feature ready? I need to exclude a table.

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 11, 2023

It is in EF Core Power Tools CLI, yes. But not in the standard CLI tooling @MikeMcWilliams

@MikeMcWilliams
Copy link

MikeMcWilliams commented Jul 11, 2023

It is in EF Core Power Tools CLI, yes. But not in the standard CLI tooling @MikeMcWilliams

Thanks @ErikEJ. That unfortunately is not an option for me, I am using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore in my partial because scaffolding does not include using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore; in its dbcontext output.

I am only looking to skip the DataProtectionKeys table; but I wouldn't need it if the scaffolding detected the DataProtectionKeys table and then added the supporting using statement.

image

See, I have a partial dbcontext that inherits from IDataProtectionKeyContext which resides in the 'Microsoft.AspNetCore.DataProtection.EntityFrameworkCore' namespace. Since the scaffolding does not seem to support this natively, the DataProtectionKeys table is scaffolded; so I am trying to tell scaffolding to skip DataProtectionKeys.

Is there a way to tell the scaffolding that I'm using DataProtection? This seems like it would be a very common practice for apps behind a load balancer and would be used often.

@ErikEJ
Copy link
Contributor

ErikEJ commented Jul 11, 2023

@MikeMcWilliams How would the naive tooling support help here???

@MaherJendoubi
Copy link

Is there any update for this feature?

@ajcvickers
Copy link
Contributor

This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 8.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests