2017-06-15 83 views
0

我需要更改模型中具有默認值設置的特定列的數據類型。更改具有默認值的mvc模型屬性的數據類型

public class Customer 
{ 
    public int CustomerId { get; set; } 

    [MaxLength(50, ErrorMessage ="Please enter customer's last name")] 
    [Display(Name = "Last name")] 
    [Column(TypeName = "varchar")] 
    public string Lastname { get; set; } 

    [MaxLength(50, ErrorMessage = "Please enter customer's first name")] 
    [Display(Name = "First name")] 
    [Column(TypeName = "varchar")] 
    public string Firstname { get; set; } 

    [Column(TypeName = "float")] 
    public double Points { get; set; } 

    public string UserId { get; set; } 
    [Display(Name = "Id Type Presented")] 
    public int IdTypeId { get; set; } 

    [ForeignKey("UserId")] 
    public virtual ApplicationUser User { get; set; } 

    [ForeignKey("IdTypeId")] 
    [Display(Name = "Id Type")] 
    public virtual IdType IdType { get; set; } 

    [Required] 
    [Display(Name = "Status")] 
    public int StatusTypeId { get; set; } 

    [ForeignKey("StatusTypeId")] 
    [Display(Name = "Status")] 
    public virtual StatusType StatusType { get; set; } 

    [Required] 
    [Display(Name = "Id Number")] 
    [MaxLength(20, ErrorMessage = "Id number is required for verification")] 
    [Column(TypeName = "varchar")] 
    public string IdNumber { get; set; } 

    public Customer() 
    { 
     this.Lastname = ""; 
     this.Firstname = ""; 
     this.IdNumber = ""; 
    } 
} 

在上面的構造函數中的3列阻止我更新數據庫,因爲它說:

The object 'DF__Customers__IdNum__31EC6D26' is dependent on column 'IdNumber'. 
ALTER TABLE ALTER COLUMN IdNumber failed because one or more objects access this column. 

這裏是產生遷移腳本

public partial class setdatatypestovarcharincustomerstablewhereapplicable : DbMigration 
{ 
    public override void Up() 
    { 

     AlterColumn("dbo.Customers", "Lastname", c => c.String(maxLength: 50, unicode: false)); 
     AlterColumn("dbo.Customers", "Firstname", c => c.String(maxLength: 50, unicode: false)); 
     AlterColumn("dbo.Customers", "IdNumber", c => c.String(nullable: false, maxLength: 20, unicode: false)); 
    } 

    public override void Down() 
    { 
     AlterColumn("dbo.Customers", "IdNumber", c => c.String(nullable: false, maxLength: 20)); 
     AlterColumn("dbo.Customers", "Firstname", c => c.String(maxLength: 50)); 
     AlterColumn("dbo.Customers", "Lastname", c => c.String(maxLength: 50)); 
    } 
} 

我該怎麼做?我可以將約束放入遷移腳本中然後重新創建嗎?

謝謝

+0

首先我要說的是刪除構造函數。你也可以在屬性值的末尾做默認值,就像'public string Lastname {get;組; } =「」;'但是你真的已經回答了你自己的問題,因爲這個問題與其他人沒有任何關係。那麼爲什麼不直接刪除呢? – Edward

回答

0

這就是我所做的。在遷移腳本中設置defaultValue。

public override void Up() 
{    
    AlterColumn("dbo.Customers", "Lastname", c => c.String(nullable: false, maxLength: 50, unicode: false, defaultValue: "")); 
    AlterColumn("dbo.Customers", "Firstname", c => c.String(nullable: false, maxLength: 50, unicode: false, defaultValue: "")); 
    AlterColumn("dbo.Customers", "IdNumber", c => c.String(nullable: false, maxLength: 20, unicode: false, defaultValue: "")); 
}