相关疑难解决方法(0)

映射时转换值

我正在使用EF Code-First到现有的数据库方法,并IsActive在我的数据库中有一个字段.问题是该领域VARCHAR应该是什么时候boolean.我无法更改数据库架构.

数据库中的示例值为"Y" (true)或"N" (false)

映射时,我想将这些值转换为true/false,并使我的Entity类保持布尔值.

这可能吗?

我的实体和映射类如下,但我想将该IsActive字段更改为布尔值.

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");
    } …
Run Code Online (Sandbox Code Playgroud)

entity-framework asp.net-mvc-4

35
推荐指数
1
解决办法
3万
查看次数

实体框架代码首先在类布尔值和列整数之间进行转换

我正在使用Entity Framework 5 code first. 我的表有一个名为的列Active,它的数据类型是类型int。存储在 Active 中的值为01null

我有一个类需要映射到这个表。

public class CommandExecutionServer : IEntity
{
     public int Id { get; set; }

     public bool? IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我的配置文件。我试图将我的类中的布尔属性映射到数据库中的整数字段。

class CommandExecutionServerConfiguration : EntityTypeConfiguration<CommandExecutionServer>
{
     internal CommandExecutionServerConfiguration()
     {
          this.ToTable("tblCommandExecutionServers");
          this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("bit");
     }
}
Run Code Online (Sandbox Code Playgroud)

这不是很好。我得到的错误是:

The 'IsActive' property on 'CommandExecutionServer' could not be set to a 'Int32' value. You must set this property to a non-null value …
Run Code Online (Sandbox Code Playgroud)

ado.net entity-framework entity-framework-4 entity-framework-4.1 entity-framework-5

5
推荐指数
1
解决办法
1万
查看次数