更改具有默认值的mvc模型的属性的数据类型

时间:2017-06-15 02:25:24

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

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

from math import exp,pi
import numpy as np
import matplotlib.pyplot as plt

def midpoint(f,a,b,n):
    h = float((b - a) / n)
    result = 0
    for i in range(n):
        result += f((a + h / 2.0) + i * h)
    result *= h
    return result

g = lambda y: ((exp(-y) * y ** 3) / (1 - exp(-y)))
a = 0
b = 1000
n = 2 ** np.arange(1, 20)
f = np.vectorize(midpoint)
m = f(g, a, b, n)
r = (pi**4/15)

plt.plot(n, m, label="numberical")
ax = plt.gca()
ax.set_xscale('log')
ax.set_ylim(auto=False)
xmin, xmax = plt.xlim()
plt.plot([xmin, xmax], [r, r], label="analytical")
plt.xlabel('n')
plt.ylabel('intergral')
plt.legend()
plt.show()

上面构造函数中的3列阻止我更新数据库,因为它说:

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 = "";
    }
}

以下是生成的迁移脚本

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.

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

由于

1 个答案:

答案 0 :(得分: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: ""));
}