-
-
Notifications
You must be signed in to change notification settings - Fork 297
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 #16
Comments
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 @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; }
} |
OK. since I just get the generated files, maybe it would be better with
Do a PR, perhaps? |
From @mbeckenbach on February 6, 2018 6:54 I have never edited a vs extension. But i'll try at weekend. :-) |
Great, let me know if you hit any stumbling blocks! (It is a bit more involved than a standard Nuget library) |
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. |
@tonysneed Thanks. I assume that this will require changes to the users product code, and does not apply to EF Core Power Tools?? |
Just need to check, how are helper and transform methods are added when using EF Core PT’s?
|
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 |
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: Could you please clarify what's the recommended way of mapping Enums with EF Core Power Tools? Thanks! |
@AlexVPerl create a NonMapped enum property in a partial class |
@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. |
At the moment this is not supported. Happy to receive ideas or a PR for implementation. |
@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 |
|
Create a proper, surrogate primary key? |
Custom enum mapping is now available with EF Core 7 and T4 templates |
From @mbeckenbach on February 3, 2018 9:40
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?
Copied from original issue: ErikEJ/SqlCeToolbox#621
The text was updated successfully, but these errors were encountered: