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

Custom type mapping #621

Closed
mbeckenbach opened this issue Feb 3, 2018 · 6 comments
Closed

Custom type mapping #621

mbeckenbach opened this issue Feb 3, 2018 · 6 comments

Comments

@mbeckenbach
Copy link

This project is awesome! It feels just like the db first workflow is coming back. But there is one feature that I am really missing.

In EF6 DB First, there is this amazing feature of mapping external types to properties.
This can be perfectly be used for transforming an integer culumn into a C# enum.

EF Core supports enum properties and maps those to integer columns in the database.

When doing a reverse engineer, all integer columns get generated as integer properties as it should be. Then one can simply change a properties type to some enum type, which exists in the project. Works great.

But when repeating the reverse engineer process to update the generated model from database, the file will be overwritten, which removes the modification of the properties type.

So I was thinking about how it would be possible to preserve this modification.

One way of doing this could be to create a code template for a specific class file. I had a look at EntityFrameworkCore.Scaffolding.Handlebars by @tonysneed but I dont see a way for doing this.

Another idea was to extend the efpt.config.json with an array of type mappings, that could be applied after code generation. Could something like that be implemented in your tool?

{
  "ContextClassName": "EfcoretestContext",
  "DatabaseType": 2,
  "IdReplace": false,
  "IncludeConnectionString": true,
  "OutputPath": "Models",
  "ProjectRootNamespace": "EFCoreTest",
  "SelectedToBeGenerated": 0,
  "Tables": [ "dbo.Posts", "dbo.Users" ],
  "UseDatabaseNames": false,
  "UseFluentApiOnly": true,
  "UseHandleBars": true,
  "UseInflector": true,
  "TypeMappings": [
    { "Column": "dbo.Posts.Category", "Type": "Enums.Categories" }
  ]
}
@ErikEJ
Copy link
Owner

ErikEJ commented Feb 3, 2018

Anything goes, and the tool is open source and welcomes contributions!

Could you show an example of what a class with a Enum would look like before and after this?

@mbeckenbach
Copy link
Author

Hi @ErikEJ, good to hear that.

A typical example for my use case would be like this. You have a custom enum in some folder/namespace and a generated class like the ones below.

namespace EFCoreTest.Enums
{
    public enum Categories
    {
        SomeCategory = 0,
        AnotherCategory = 1,
        MoreCategories = 2
    }
}
namespace EFCoreTest.Models
{
    public partial class Post
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public int Category { get; set; }
    }
}

After transformation the class would simply look like this:

public partial class Post
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public Enums.Categories Category { get; set; }
    }

@ErikEJ
Copy link
Owner

ErikEJ commented Feb 4, 2018

OK. since I just get the generated files, maybe it would be better with

 "TypeMappings": [
    { "Class:"Post", "Property":"Category", "Type": "Enums.Categories" }
  ]

Do a PR, perhaps?

https://github.com/ErikEJ/SqlCeToolbox/blob/master/src/GUI/ReverseEngineer20/EFCoreReverseEngineer.cs#L130

@mbeckenbach
Copy link
Author

I have never edited a vs extension. But i'll try at weekend. :-)
Thanks for the hint on the line.

@ErikEJ
Copy link
Owner

ErikEJ commented Feb 6, 2018

Great, let me know if you hit any stumbling blocks! (It is a bit more involved than a standard Nuget library)

@ErikEJ
Copy link
Owner

ErikEJ commented May 19, 2018

This issue was moved to ErikEJ/EFCorePowerTools#16

@ErikEJ ErikEJ closed this as completed May 19, 2018
@ErikEJ ErikEJ removed this from the Backlog milestone Aug 13, 2019
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

2 participants