TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

Scaffold EF Core models using Handlebars templates.
MIT License
210 stars 53 forks source link

How to use EnumToNumeric Converter with Handlebar? #38

Closed noufionline closed 5 years ago

noufionline commented 5 years ago

Is there any way we can configure the converter with the handlebar?

tonysneed commented 5 years ago

@noufionline Sorry for my slow response. I assume you are referring to EF Core Value Conversions, which can be defined in the OnModelCreating method of the DbContext class. In this case you need to inject code into the OnModelCreating method. The way this is traditionally done is via partial methods.

First place the OnModelCreatingExt partial method in the DbContext.hbs file, which will inject the partial method definition into your generated DbContext class.

{{{on-model-creating}}}

partial void OnModelCreatingExt(ModelBuilder modelBuilder);
    }
}

Then create a partial class file for your generated DbContext class, in which you implement your partial method.

public partial class NorthwindSlimContext
{
    partial void OnModelCreatingExt(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Product>()
            .Property(e => e.CategoryName)
            .HasConversion(
                v => v.ToString(),
                v => (CategoryEnum)Enum.Parse(typeof(CategoryEnum), v));
    }
}

Please let me know if this adequately addresses your question. Cheers.

tonysneed commented 5 years ago

I updated the latest version and sample to include this scenario. Feel free to re-open this issue if you have further questions.

noufionline commented 5 years ago

Hi @tonysneed, Thanks a lot. My requirement is I have a PaymentStatus field which is int in SQL Server but in the model generated I wanted as an Enum. How is that possible while reverse engineering?

tonysneed commented 5 years ago

@noufionline The conversion then needs to be between an int and your enum. For example:

public partial class NorthwindSlimContext
{
    partial void OnModelCreatingExt(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Product>()
            .Property(e => e.CategoryId)
            .HasConversion(
                v => (int)v,
                v => (CategoryEnum)v);
    }
}
tonysneed commented 5 years ago

@noufionline Let me know if this answers your question. Be sure to check out the sample in this repository.

noufionline commented 5 years ago

Hi @tonysneed ,

I think I was not clear with my question. I want the reverse engineered model to have enum property instead of int property.

instead of

public int OrderStatus {get; set} should be public OrderStatusEnum OrderStatus {get; set;}

Hope it helps.

Thanks in Advance.

Noufal

tonysneed commented 5 years ago

Yes, this is what the sample illustrates. Have you looked at it?

noufionline commented 5 years ago

Thank you so much @tonysneed. This is what exactly I wanted. Much appreciated.