Mat*_*ete 7 c# entity-framework-core asp.net-core
我正在使用Entity Framework 7处理我的第一个项目,并且正在连接到已经创建了数据库的SQL Server,但它还没有表.我创建了我的DbContext并创建了一个类,然后DbSet<>在我的上下文中设置了一个.我运行命令以启用迁移并创建第一次迁移,然后使用rand命令更新数据库.一切看起来都很好,没有出现任何错误,但是当我查看数据库时,只EFMigraitonsHistory创建了表.当我查看为初始迁移创建的类时,它基本上是空白的.我究竟做错了什么?
我正在运行的命令:
dnvm install latest -r coreclr
dnx ef migrations add MyFirstMigration
dnx ef database update
Run Code Online (Sandbox Code Playgroud)
语境:
namespace JobSight.DAL
{
public class JobSightDBContext : DbContext
{
public DbSet<NavigationMenu> NavigationMenu { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
表类:
namespace JobSight.DAL
{
public class NavigationMenu
{
[Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int16 ID { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }
public string ExternalURL { get; set; }
public string Title { get; set; }
public Int16? ParentID { get; set; }
public virtual NavigationMenu Parent { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<JobSightDBContext>(options =>
{
options.UseSqlServer(Configuration["Data:JobSightDatabase:ConnectionString"]);
});
}
Run Code Online (Sandbox Code Playgroud)
初始迁移类(由EF自动生成):
namespace JobSight.WebUI.Migrations
{
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:在做了Poke的建议后,这是我新的自动生成的迁移.但是,该表仍未在数据库级别创建.
namespace JobSight.WebUI.Migrations
{
public partial class MyFirstMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "NavigationMenu",
columns: table => new
{
ID = table.Column<short>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
ActionName = table.Column<string>(nullable: true),
ControllerName = table.Column<string>(nullable: true),
ExternalURL = table.Column<string>(nullable: true),
ParentID = table.Column<short>(nullable: true),
Title = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_NavigationMenu", x => x.ID);
table.ForeignKey(
name: "FK_NavigationMenu_NavigationMenu_ParentID",
column: x => x.ParentID,
principalTable: "NavigationMenu",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable("NavigationMenu");
}
}
}
Run Code Online (Sandbox Code Playgroud)
pok*_*oke 15
您需要首先在数据库上下文中设置实体.至少,您需要这样做:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<NavigationMenu>();
}
Run Code Online (Sandbox Code Playgroud)
您的项目布局中隐藏了迁移问题.所以你拥有的是一个JobSight.DAL包含实体和数据库上下文的项目.然后你有一个项目JobSight.WebUI,它是包含Startup.cs数据库设置的实际ASP项目.
这会导致问题,因为默认情况下,EF只会假设查找当前程序集中的所有内容.因此,如果ef要从Web项目启动命令,即使上下文位于另一个项目中,它也会在那里创建迁移.但是,当您尝试应用迁移时,EF将无法找到它,因为它只会查看上下文的项目.
因此,要解决此问题,您需要在DAL项目中创建迁移.您可以通过在调用ef命令时指定项目来执行此操作:
dnx ef migrations add Example -p JobSight.DAL
Run Code Online (Sandbox Code Playgroud)
您可以通过dnx ef migrations list以后运行来验证这是否有效.现在应该返回Example迁移; 以前,该命令没有返回任何内容:它找不到迁移,这就是为什么update命令只说Done(没有应用迁移)并且没有创建数据库的原因.因此,如果您现在可以在那里进行迁移,则可以使用以下方法应用它:
dnx ef database update
Run Code Online (Sandbox Code Playgroud)
请注意,由于现在在DAL项目中创建了迁移,因此需要添加对EntityFramework.MicrosoftSqlServer该项的引用,否则项目将无法编译.您需要先执行此操作,然后才能运行上述list命令.
最后,有关此内容的更多信息,请参阅此问题.
| 归档时间: |
|
| 查看次数: |
12432 次 |
| 最近记录: |