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

Exclude Tables #3862

Closed
MikeMcWilliams opened this issue May 12, 2022 · 10 comments
Closed

Exclude Tables #3862

MikeMcWilliams opened this issue May 12, 2022 · 10 comments

Comments

@MikeMcWilliams
Copy link

In a project, I am leveraging DataProtectionKey from using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore and unable to exclude the DataProtectionKeys table from scaffolding. I have a partial class file that is intended not to be a part of the scaffolding. How can the DataProtectionKeys table be ignored in the scaffolding? (It's not optimal to maintain a table (--table option) to list the tables for scaffolding) Please advise.

MyContextPartial.cs

using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public partial class MyContext : DbContext, IDataProtectionKeyContext
{
   virtual public DbSet<DataProtectionKey> DataProtectionKeys { get; set; } = null!; <--This is the correct implementation from the using/namespace above.
}

MyContext.cs
This file is scaffolded by ef-core and contains all the tables including DataProtectionKeys which should be skipped/ignored.

public partial class MyContext : DbContext, IDataProtectionKeyContext
{
    public virtual DbSet<DataProtectionKey> DataProtectionKeys { get; set; }  <--This property (table) should not be scaffolded/excluded
    ... other scaffolded tables
}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ErikEJ
Copy link
Contributor

ErikEJ commented May 12, 2022

Try EF Core Power Tools, has a UI and persistence of table selection

@MikeMcWilliams
Copy link
Author

Try EF Core Power Tools, has a UI and persistence of table selection

Thank you for the rapid reply! Unfortunately we need to be able to do this without a UI for our CI/CD pipeline. Table exclusion on DataProtectionKey(s) (a microsoft .net class appears to be the best option.)
Right now, I just comment the DBSet that scaffold creates in the context and remove the DataProtectionKey scaffolded class.

@ErikEJ
Copy link
Contributor

ErikEJ commented May 12, 2022

@MikeMcWilliams Design time activities during CI/CD sounds dangerous to me.

@MikeMcWilliams
Copy link
Author

Our SQL Server Database Project, which is also in the .sln, directly drives the models via scaffolding.

We call it “Source First” Instead of “database first” or “code first”. The concept is that the schema (“source code” in the database project) is king. We will not use migrations and drive deployment through SQLPackage.exe. Scaffolding provides the models. Its a two or three-step process but it’s 100% scriptable. The really good news from a database perspective is that our DBAs love the approach. The Devs are new to this process and currently do manual alter scripting.

If I can figure out how to get this last piece in I think everything will be tied together beautifully.

@ErikEJ
Copy link
Contributor

ErikEJ commented May 12, 2022

Sweet. So you are using my dacpac scaffolding library.

It is possible to use EF Core Power Tools from the command line but not supported.

@MikeMcWilliams
Copy link
Author

Sweet. So you are using my dacpac scaffolding library.

It is possible to use EF Core Power Tools from the command line but not supported.

Not sure, did you create the scaffold part of dotnet ef dbcontext scaffold ? If no, what's your library? (The EF Core Power Tools?)

Thank you for keeping the dialog going!

@ErikEJ
Copy link
Contributor

ErikEJ commented May 12, 2022

I was referring to this package, that let's you scaffold directly from a .dacpac (with a few limitations) https://www.nuget.org/packages/ErikEJ.EntityFrameworkCore.SqlServer.Dacpac/

@bricelam
Copy link
Contributor

bricelam commented May 16, 2022

Hmm, without dotnet/efcore#6182, the recommend way to do this in would be, as you said, to use the --table option.

If you really want to avoid that, you could override the design-time service that reads from the database.

class MyDatabaseModelFactory : SqlServerDatabaseModelFactory
{
    public override DatabaseModel Create(string connectionString, DatabaseModelFactoryOptions options)
        => Fixup(base.Create(connectionString, options);

    public override DatabaseModel Create(DbConnection connection, DatabaseModelFactoryOptions options)
        => Fixup(base.Create(connection, options);

    DatabaseModel Fixup(DatabaseModel database)
    {
        var dataProtectionKeys = database.Tables.First(t => t.Name == "DataProtectionKeys");
        database.Tables.Remove(dataProtectionKeys);

        return database;
    }
}

It will also be able to skip generating the classes by using a custom template once we ship dotnet/efcore#4038.

@MikeMcWilliams
Copy link
Author

Are table exclusions available now?

@bricelam
Copy link
Contributor

@MikeMcWilliams Tracked by dotnet/efcore#6182

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

4 participants