Casting an integer into an enumeration in OnModelCreating in code first

Last Reply 8 days ago By pandeyism

Posted 8 days ago

In Repository Class:

public IQueryable ListPousadas()
{
    var y = _db.Set<Pousada>().AsNoTracking().Where(x => x.Deleted == false);
    return y;
}

In Enum Class:

public enum PousadaSize
{
    Small = 1, Medium = 2, Large = 4, ExtraLarge = 8
}

In OnModelCreating:(i am using this code)

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Pousada>().Property(e => e.Size)
           .HasConversion(v => v.ToString(),v => (PousadaSize)Enum.Parse(typeof(PousadaSize), v));
}

 

You are viewing reply posted by: pandeyism 8 days ago.