相关疑难解决方法(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万
查看次数

外键约束“foreign_key”中引用列“column”和引用列“Id”不兼容

我正在尝试使用实体框架生成迁移,但我不断遇到

Referencing column 'TaskId' and referenced column 'Id' in foreign key constraint 'FK_WorkerTaskTargetReference_WorkerTask_TaskId' are incompatible.
Run Code Online (Sandbox Code Playgroud)

我不知道为什么。

我的配置似乎是正确的:

public class WorkerTaskConfiguration : EntityConfiguration<WorkerTask>
    {
        public override void Configure()
        {
            Entity.Property(x => x.CreatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
            Entity.Property(x => x.UpdatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
            Entity.HasMany(x => x.Messages).WithOne(x => x.Task).HasForeignKey(x => x.TaskId).IsRequired();
            Entity.HasMany(x => x.TargetReferences).WithOne(x => x.Task).HasForeignKey(x => x.TaskId).IsRequired();
        }
}

  public class WorkerTaskTargetReferenceConfiguration : EntityConfiguration<WorkerTaskTargetReference>
    {
        public override void Configure()
        {
            Entity.HasKey(e => e.Id);
            Entity.Property(e => e.CreatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
            Entity.Property(e => e.UpdatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是当我应用迁移时:

Failed executing DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] …
Run Code Online (Sandbox Code Playgroud)

mysql entity-framework entity-framework-core

8
推荐指数
2
解决办法
2万
查看次数