我有一个本地数据库,目前它已经是第二个版本了,现在应该升级到第三个版本。
之前的迁移代码是由另一个程序员生成的,所以我认为我在这里做错了什么。
在我的模型中,大约有30个类,在模型文件夹内有一个映射文件夹,其中包含了这30个类的映射。
所以现在我按照同样的方式添加了1个新类,然后在Package Manager Console中运行add-migration命令。
不幸的是,我得到了一个空的迁移Up()和Down()方法。
当我查看数据库时,可以看到__migrationHistory可用,并且有前两个迁移。如果我现在运行我的应用程序,第三个迁移也会被添加,但很明显,新表没有被创建,因为它不在Up()方法中。
我可能做错了什么?
我认为在脚手架之前的迁移过程中出了问题......就像它找不到我添加的新的Code-First类一样。
这是我的命令:
add-migration "1.2" -verbose -ProjectName "MyEFproject"
我假设脚手架不知道在哪里寻找新类...或者所有模型类都期望在项目中,这是惯例吗?
add-migration 的结果:
namespace MyProject.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class _1002 : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
}
新模型类示例:
using System;
using System.Collections.Generic;
namespace MyProject.Models
{
public partial class MyTable
{
public string SomeId { get; set; }
public string SomeText { get; set; }
}
}
新的Mapping类示例
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
namespace MyProject.Models.Mapping
{
public class MyTableMap : EntityTypeConfiguration<MyTable>
{
public MyTableMap()
{
// Primary Key
this.HasKey(t => t.SomeId);
// Properties
this.Property(t => t.SomeText)
.IsRequired()
.HasMaxLength(30);
// Table & Column Mappings
this.ToTable("MyTable", "database");
this.Property(t => t.SomeId).HasColumnName("SomeId");
this.Property(t => t.SomeText).HasColumnName("SomeText");
}
}
}
谢谢您,
DbContext
类中是否引用了MyTable
表? - CodingIntrigue