【问题标题】:Change the datatype of a property of an mvc model that has a default value更改具有默认值的 mvc 模型的属性的数据类型
【发布时间】:2017-06-15 02:25:23
【问题描述】:

我需要更改模型中具有默认值设置的特定列的数据类型。

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

我该怎么做?我可以在迁移脚本中删除约束然后重新创建它吗?

谢谢

【问题讨论】:

  • 首先我想说的是删除构造函数。您也可以在属性值的末尾设置默认值,例如public string Lastname { get; set; } = ""; 但实际上您已经回答了自己的问题,这个问题与其他任何人都没有关系。那么为什么不直接删除呢?

标签: c# entity-framework entity-framework-migrations asp.net-mvc-5


【解决方案1】:

这就是我所做的。在迁移脚本中设置 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: ""));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多