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

Make Scaffold generate field as CLR Enum #27792

Closed
mikhail-rozum opened this issue Apr 9, 2022 · 7 comments
Closed

Make Scaffold generate field as CLR Enum #27792

mikhail-rozum opened this issue Apr 9, 2022 · 7 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@mikhail-rozum
Copy link

Hello!
I use ef core database-first approach in my project, and I have a table that is described with the class

public class Item
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int Status { get; set; }
}

public enum Status
{
  New = 1,
  InProgress = 2,
  Complete = 3
}

I want Scaffold to make Item.Status property as Status type. Is it possible to point out to use a pre-defined enum for some fields when using Scaffold?

I found a post on SO (https://stackoverflow.com/questions/44262314/how-can-i-make-ef-core-database-first-use-enums), but the answer is for code-first approach

EF Core version: 6.0.3
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: . .NET 6.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.1.1

@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 10, 2022

This is possible semi-automatic with EF Core Power Tools

@roji
Copy link
Member

roji commented Apr 11, 2022

Note that in the PostgreSQL context above we have native enum types in the database itself, which can be mapped to CLR enums (see the npgsql docs). I'm not sure if there's a place where user code could be placed for execution before reverse engineering takes place - if so, doing NpgsqlConnection.GlobalTypeMapper.MapEnum<T>() would be the right way (/cc @bricelam). This would require some work on the Npgsql side.

Otherwise, in the more general context, this could probably be achieved by customizing templates, which is being done for EF Core 7.0 (see #4038).

@mikhail-rozum
Copy link
Author

Thank you for your advices. I was thinking about using either native Postgresql enums and current int fields. But in the case with using int type I admitted to add Item.Status field manually in a partial class, but I didn't find a way to exclude fields (not whole tables) from generating by Scaffold. Is there a way to exclude field from Scaffold?

@ErikEJ
Copy link
Contributor

ErikEJ commented Apr 14, 2022

Yes excluding columns is possible with EF Core Power Tools.

@mikhail-rozum
Copy link
Author

Thank you all. I will use EF Core Power Tools

@roji roji added the closed-no-further-action The issue is closed and no further action is planned. label Apr 15, 2022
@rmcp
Copy link

rmcp commented Oct 11, 2022

Is it possible to achieve this using CLI?

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 11, 2022

@rmcp Yes, if you combine with .t4 templates.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

5 participants