2017-08-14 98 views
1

所以我有这个POCO代表我的数据库中的表:为什么使用EF迁移不能将一个布尔列持久保存到我的数据库中?

// BaseDataObject includes the Id property among others. 
public class SupplierDocument : BaseDataObject 
{ 
    public int SupplierId { get; set; } 
    public string Name { get; set; } 
    public DateTime ExpiryDate { get; set; } 
    public string MimeType { get; set; } 
    public byte[] Document { get; set; } 
    public bool Alerted { get; set; } 
} 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 

    public DbSet<PaymentTerm> PaymentTerms { get; set; } 
    public DbSet<Supplier> Suppliers { get; set; } 
    public DbSet<SupplierDocument> SupplierDocuments { get; set; } 
    public DbSet<SupplierService> SupplierServices { get; set; } 
    public DbSet<Voucher> Vouchers { get; set; } 


    public ApplicationDbContext() 
     : base("DevConnection", throwIfV1Schema: false) 
    { 
    } 
} 

我加在后面的迁移Alerted财产我已经做了一个更新到数据库:

public override void Up() 
{ 
    AddColumn("dbo.Suppliers", "Area", c => c.String()); 
    AddColumn("dbo.Vouchers", "IssuedFor", c => c.String()); 
    AddColumn("dbo.SupplierDocuments", "Alerted", c => c.Boolean()); 
} 

public override void Down() 
{ 
    DropColumn("dbo.Vouchers", "IssuedFor"); 
    DropColumn("dbo.Suppliers", "Area"); 
    DropColumn("dbo.SupplierDocuments", "Alerted"); 
} 

矛盾的是,当我在添加迁移后添加到update-database时,该列不会被添加到表中。

事情是,我不得不手动添加列,因为它不包括在add-migration之后的默认迁移中。

缺少删除所有的迁移并重新开始使用当前模型(经验告诉我不能保证解决问题),我怎么能做到这一点,为什么它不工作呢?

+0

性能我甚至不能说得出结论,出现这种情况只有* *与布尔字段。这次就是这种情况。 – Ortund

+0

我在web.config中有另一个名为*“LiveConnection”*的连接字符串,它的目标是一个远程数据库而不是。\ SQLEXPRESS – Ortund

+0

'update-database -verbose'显示什么? –

回答

1

由于您已经在该特定表中有记录,因此您可能会在后台发现错误,提示可能会丢失数据。

尝试提供“可空”和“默认值”,在AddColumn声明

AddColumn("dbo.SupplierDocuments", "Alerted", c => c.Boolean(nullable: false, defaultValue: true)); 
相关问题