映射时转换值 [英] Convert value when mapping

查看:107
本文介绍了映射时转换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 EF Code-First到现有数据库方法,并在我的数据库中有一个 IsActive 字段。问题是当它应该是一个 boolean 时,该字段是 VARCHAR 。我不能更改数据库模式。

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.

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

Example value in the database are "Y" (true) or "N" (false)

当映射时,我想将这些值转换为true / false,并将我的Entity类保留为布尔值<

When mapping, I want to convert those values to either true/false and keep my Entity class with the boolean value.

这是可能吗?

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

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");
    }
}

编辑:没有其他解决方案: https://stackoverflow.com/a/6709186/1053611

I found no other solution than this: https://stackoverflow.com/a/6709186/1053611

推荐答案

正如其他人指出,您需要两个属性,但您可能有兴趣知道您可以将其中一个属性设为私有,并将其映射到数据库:

As others have pointed out you need two properties, but you may be interested to know that you can make one of the properties private and still map it to the database:

    private string isActive { get; set; }

    [System.ComponentModel.DataAnnotations.Schema.NotMapped]
    public bool IsActive
    {
        get { return isActive == "Y"; }
        set { isActive = value ? "Y" : "N"; }
    }

如果你使用的是EF6,你可以在 OnModelCreating 方法映射私有属性

If you are using EF6 you can use a custom convention in the OnModelCreating method to map the private property

modelBuilder.Types().Configure(c =>
{
    //NB the syntax used here will do this for all entities with a 
    //private isActive property
    var properties = c.ClrType.GetProperties(BindingFlags.NonPublic 
                                             | BindingFlags.Instance)
                              .Where(p => p.Name == "isActive");
    foreach (var p in properties)
        c.Property(p).HasColumnName("IsActive");
});

参考文献:

使用自定义约定映射私有属性

映射没有自定义约定的私有属性(EF6之前)

编辑:

这是另一种识别应映射到数据库的私有属性的方法:

Here's another way of identifying the private properties that should be mapped to the database:

首先将列属性添加到私人财产:

First add the column attribute to the private property:

[System.ComponentModel.DataAnnotations.Schema.Column]
private string isActive { get; set; }

然后使用该属性的存在来识别您的 OnModelCreating中的私有属性方法:

Then use the presence of that attribute to identify private properties in your OnModelCreating method:

modelBuilder.Types().Configure(c =>
{
    var properties = c.ClrType
        .GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
        .Where(propInfo => 
           propInfo.GetCustomAttributes(typeof(ColumnAttribute), true).Length > 0);

    foreach (var p in properties)
        c.Property(p).HasColumnName(p.Name);
});

参考:使用实体框架映射私有财产

这篇关于映射时转换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆