I'm using EF Code-First to an existing database method and have a IsActive
field in my database. The problem is that the field is VARCHAR
when it should be a boolean
. I can't change the Database schema.
Example value in the database are "Y" (true) or "N" (false)
When mapping, I want to convert those values to either true/false and keep my Entity class with the boolean value.
Is this possible?
My Entity and mapping classes are the following but I would like to change the IsActive
field to be a boolean.
public class Employee
{
public int ID { get; set; }
public string SSN { get; set; }
public string Email { get; set; }
public string IsActive { get; set; }
}
public class EmployeeMap : EntityTypeConfiguration<Employee>
{
public EmployeeMap()
{
this.ToTable("Employees");
this.HasKey(t => t.ID);
this.Property(t => t.ID).HasColumnName("ID_Employee");
this.Property(t => t.SSN).HasColumnName("sReference");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.IsActive).HasColumnName("IsActive");
}
}
EDIT: I found no other solution than this: https://mcmap.net/q/409951/-convert-from-to-string-in-database-to-boolean-property-entity-framework-4-1
IsActive=="Y"?true:false
and optional setter withIsActive=value?"Y":"N"
... – Fasciculus