In ASP.NET Core, I have this enum:
public enum MerchantStatus : byte
{
Inactive = 0,
Active = 1
}
Model:
public class Merchant : AuditableBaseEntity
{
[Required(ErrorMessage = "Merchant Name is required")]
[Column(TypeName = "Varchar")]
[StringLength(100)]
public string MerchantName { get; set; }
[Required(ErrorMessage = "Account Number is required")]
[Column(TypeName = "Varchar")]
[StringLength(50)]
public string AccountNumber { get; set; }
public MerchantStatus? MerchantStatus { get; set; } // 0=Inactive, 1=Active Active/Inactive --default value
[Display(Name = "Charge Mode")]
public ChargeMode? ChargeMode { get; set; } //0=Fixed Or 1=Percentage
}
modelBuilder.Entity<Merchant>(entity =>
{
entity.Property(m => m.Id).ValueGeneratedOnAdd();
entity.Property(m => m.MerchantStatus).HasDefaultValue(1);
});
I got this error:
Cannot set default value '1' of type 'System.Int32' on property 'MerchantStatus' of type 'System.Nullable`1[Helpers.EnumList MerchantStatus]' in entity type 'Merchant'.
How do I resolve this?
Thaanks
CodePudding user response:
cast your integer by replacing
.HasDefaultValue(1);
with .HasDefaultValue((MerchantStatus)1);
or better - use the defined enum .HasDefaultValue(MerchantStatus.Active)