Code Monkey home page Code Monkey logo

Comments (8)

tonysneed avatar tonysneed commented on September 13, 2024 2

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

from entityframeworkcore.scaffolding.handlebars.

noufionline avatar noufionline commented on September 13, 2024 1

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

from entityframeworkcore.scaffolding.handlebars.

tonysneed avatar tonysneed commented on September 13, 2024

@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.

from entityframeworkcore.scaffolding.handlebars.

tonysneed avatar tonysneed commented on September 13, 2024

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

from entityframeworkcore.scaffolding.handlebars.

noufionline avatar noufionline commented on September 13, 2024

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?

from entityframeworkcore.scaffolding.handlebars.

tonysneed avatar tonysneed commented on September 13, 2024

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

from entityframeworkcore.scaffolding.handlebars.

tonysneed avatar tonysneed commented on September 13, 2024

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

from entityframeworkcore.scaffolding.handlebars.

noufionline avatar noufionline commented on September 13, 2024

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

from entityframeworkcore.scaffolding.handlebars.

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.