Closed noufionline closed 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.
I updated the latest version and sample to include this scenario. Feel free to re-open this issue if you have further questions.
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?
@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);
}
}
@noufionline Let me know if this answers your question. Be sure to check out the sample in this repository.
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
Yes, this is what the sample illustrates. Have you looked at it?
Thank you so much @tonysneed. This is what exactly I wanted. Much appreciated.
Is there any way we can configure the converter with the handlebar?