C# 更改具有默认值的mvc模型属性的数据类型
我需要更改模型中具有默认值设置的特定列的数据类型C# 更改具有默认值的mvc模型属性的数据类型,c#,entity-framework,entity-framework-migrations,asp.net-mvc-5,C#,Entity Framework,Entity Framework Migrations,Asp.net Mvc 5,我需要更改模型中具有默认值设置的特定列的数据类型 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 La
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));
}
}
我该怎么做?我可以在迁移脚本中删除约束,然后重新创建它吗
谢谢以下是我所做的。在迁移脚本中设置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: ""));
}
首先,我要说的是删除构造函数。您还可以在属性值的末尾执行默认值,如so
publicstringlastname{get;set;}=“”但实际上你已经回答了你自己的问题,这个问题可能与其他人没有任何关系。那么为什么不干脆删除它呢?