Code Monkey home page Code Monkey logo

Comments (17)

ErikEJ avatar ErikEJ commented on August 23, 2024 1

Create a proper, surrogate primary key?

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

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?

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

From @mbeckenbach on February 4, 2018 7:57

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; }
    }

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

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

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

From @mbeckenbach on February 6, 2018 6:54

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

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

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

from efcorepowertools.

tonysneed avatar tonysneed commented on August 23, 2024

My Handlebars library handles this scenario now. See the project ReadMe for an example of adding an enum transformation and mapping it with a partial method.

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

@tonysneed Thanks. I assume that this will require changes to the users product code, and does not apply to EF Core Power Tools??

from efcorepowertools.

tonysneed avatar tonysneed commented on August 23, 2024

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

There are many options to do this, while preserving the modifications when reverse engineering is run a second time:

For the Enum, create at MyTypeEnum property in the partial entity class.

For special translations, use the OnModelCreating partial method to modify the model with Value Conversions https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions

from efcorepowertools.

AlexVPerl avatar AlexVPerl commented on August 23, 2024

Thank you for creating this tool. I was looking for Enum mapping as well and came across this post.

I tried adding the following to efpt.renaming.json but it didn't have any effect:
"TypeMappings": [
{ "Class:"Post", "Property":"Category", "Type": "Enums.Categories" }
]

Could you please clarify what's the recommended way of mapping Enums with EF Core Power Tools? Thanks!

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

@AlexVPerl create a NonMapped enum property in a partial class

from efcorepowertools.

AlexVPerl avatar AlexVPerl commented on August 23, 2024

@ErikEJ thank you for prompt reply. Do you mean an Enum wrapper property that would set/get persisted numeric value?

If so, I was trying to avoid that since then EF will do in memory filtering when Enum is mentioned in the LINQ query.

Above, there is a mention of using OnModelCreating partial method with Value Conversions - this would be ideal. But it doesn't seem possible since all columns will be mapped in the auto generated class, and EF won't allow mapping 2 properties to single column. Having a way to ignore a column when using Rev. Eng. auto generation would make it possible to manually do Value Conversions for it. Or is there another way?

Thanks.

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

At the moment this is not supported. Happy to receive ideas or a PR for implementation.

from efcorepowertools.

jamshally avatar jamshally commented on August 23, 2024

@ErikEJ thank you for prompt reply. Do you mean an Enum wrapper property that would set/get persisted numeric value?

If so, I was trying to avoid that since then EF will do in memory filtering when Enum is mentioned in the LINQ query.

Above, there is a mention of using OnModelCreating partial method with Value Conversions - this would be ideal. But it doesn't seem possible since all columns will be mapped in the auto generated class, and EF won't allow mapping 2 properties to single column. Having a way to ignore a column when using Rev. Eng. auto generation would make it possible to manually do Value Conversions for it. Or is there another way?

Thanks.

@AlexVPerl , @ErikEJ - I am also looking for a way to Map an enum value to a string. If I understand correctly, the one thing blocking @AlexVPerl's suggested solution was the ability to ignore a column. Well, it seems that this feature was recently added via a pull request: #572

I'm going to give that a go, but leaving this train of thought here for others to help save them some time. I'll make another note if I have anything interesting to report

from efcorepowertools.

jamshally avatar jamshally commented on August 23, 2024

I'm going to give that a go, but leaving this train of thought here for others to help save them some time. I'll make another note if I have anything interesting to report

  • Well, for me, good news and bad news... it looks like ignoring a column is new baked right into the UI! (Nice feature@ErikEJ)
  • In my case, it seems I can not ignore the column I want to transform into an Enum because it part of the primary key... so, I will find a different approach

from efcorepowertools.

ErikEJ avatar ErikEJ commented on August 23, 2024

Custom enum mapping is now available with EF Core 7 and T4 templates

from efcorepowertools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.