不确定我是否完全在同一页面上,但我认为这就是您要问的。我已将 AutomaticMigrations 设置为 false:
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
我有一个像这样设置的 Post 类:
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public string Tags { get; set; }
public ICollection<Comment> Comments { get; set; }
}
它已经生成了,但后来我意识到出于某种原因我想要标题。
public class Post
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public string Tags { get; set; }
public ICollection<Comment> Comments { get; set; }
}
我进行更改,快速构建,然后从 PM 控制台输入:
Add-Migration AddPostAnnotation(名称可以是任何你想要的)
这会生成这个文件:
public partial class AddPostAnnotations : DbMigration
{
public override void Up()
{
AlterColumn("dbo.Posts", "Title", c => c.String(nullable: false));
}
public override void Down()
{
AlterColumn("dbo.Posts", "Title", c => c.String());
}
}
在这里,我只需从 PM 控制台运行 Update-Database,然后将更改发送过来。