如何获得 Entity Framework Code First Migrations 来查看我的模型?
How do I get Entity Framework Code First Migrations to see my models?
我创建了一个新的 ASP.Net Web 应用程序并在其上启用了迁移。我 运行 add-migration initial
和 initial
迁移实际上具有用于身份验证的所有默认表(dbo.AspNetRoles、dbo.AspNetUserRoles 等)。但是,当我创建自己的上下文并向其添加实体模型时,我无法进行迁移以确认该模型。也就是说,当我 运行 add-migration added-watchedgame-model
我只是得到一个 "empty" 迁移文件。那我做错了什么?是否必须以某种方式引用我的 DbContext? Entity Framework 只能处理 1 个 dbcontext 的迁移吗?
ReleaseDateMailerDBContext.cs:
using System.Data.Entity;
using WebApplication4.Models;
namespace WebApplication4.DataAccess
{
public class ReleaseDateMailerDBContext : DbContext
{
public ReleaseDateMailerDBContext() : base("DefaultConnection") { }
public DbSet<WatchedGameModel> WatchedGameModelSet { get; set; }
}
}
WatchedGameModel.cs:
using System.ComponentModel.DataAnnotations;
namespace WebApplication4.Models
{
public class WatchedGameModel
{
public int ID { get; set; }
[MaxLength(1024)]
public string URL { get; set; }
public string Email { get; set; }
public bool EmailSent { get; set; }
}
}
"empty" 迁移文件:
namespace ReleaseDateMailer.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class addedwatchedgamemodel : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
}
"Batch Clean" 可能会解决您的问题。
这表明 EF tooling/assemblies 正在查找默认构建输出位置(通常为 /bin/Debug)以外的位置。 clean 命令也会顺便清除中间输出。
要进行批量清理:
- Select
Build
-> Batch Build
- 点击
Select All
- 点击
Clean
关闭对话框,重建并重新尝试迁移。
在 运行 执行添加迁移命令时,您的程序包管理器控制台应指向具有您的 DBContext class (WebApplication4.DataAccess) 的项目。
如果您在与 Web 应用程序项目不同的项目中进行迁移(假设 WebApplication4.Web),那么您应该 运行 以下命令:
add-migration "MigrationName" -projectName:WebApplication.DataAccess -startupProjectName:WebApplication4.Web
希望对您有所帮助!!
- 使用内置的asp.net mvc 项目,DbContext class (
ApplicationDbContext
) 已经
已创建!
- 当您输入
enable-migrations
时,会根据找到的 dbcontext class 创建一个迁移配置 class。
- 当您输入
add-migration "migrationname"
时,将检查该 dbcontext class 是否存在差异。
因此,人们所要做的就是,与其自己制作源自 DbContext
的 class,不如使用那个。
我创建了一个新的 ASP.Net Web 应用程序并在其上启用了迁移。我 运行 add-migration initial
和 initial
迁移实际上具有用于身份验证的所有默认表(dbo.AspNetRoles、dbo.AspNetUserRoles 等)。但是,当我创建自己的上下文并向其添加实体模型时,我无法进行迁移以确认该模型。也就是说,当我 运行 add-migration added-watchedgame-model
我只是得到一个 "empty" 迁移文件。那我做错了什么?是否必须以某种方式引用我的 DbContext? Entity Framework 只能处理 1 个 dbcontext 的迁移吗?
ReleaseDateMailerDBContext.cs:
using System.Data.Entity;
using WebApplication4.Models;
namespace WebApplication4.DataAccess
{
public class ReleaseDateMailerDBContext : DbContext
{
public ReleaseDateMailerDBContext() : base("DefaultConnection") { }
public DbSet<WatchedGameModel> WatchedGameModelSet { get; set; }
}
}
WatchedGameModel.cs:
using System.ComponentModel.DataAnnotations;
namespace WebApplication4.Models
{
public class WatchedGameModel
{
public int ID { get; set; }
[MaxLength(1024)]
public string URL { get; set; }
public string Email { get; set; }
public bool EmailSent { get; set; }
}
}
"empty" 迁移文件:
namespace ReleaseDateMailer.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class addedwatchedgamemodel : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
}
"Batch Clean" 可能会解决您的问题。
这表明 EF tooling/assemblies 正在查找默认构建输出位置(通常为 /bin/Debug)以外的位置。 clean 命令也会顺便清除中间输出。
要进行批量清理:
- Select
Build
->Batch Build
- 点击
Select All
- 点击
Clean
关闭对话框,重建并重新尝试迁移。
在 运行 执行添加迁移命令时,您的程序包管理器控制台应指向具有您的 DBContext class (WebApplication4.DataAccess) 的项目。 如果您在与 Web 应用程序项目不同的项目中进行迁移(假设 WebApplication4.Web),那么您应该 运行 以下命令:
add-migration "MigrationName" -projectName:WebApplication.DataAccess -startupProjectName:WebApplication4.Web
希望对您有所帮助!!
- 使用内置的asp.net mvc 项目,DbContext class (
ApplicationDbContext
) 已经 已创建! - 当您输入
enable-migrations
时,会根据找到的 dbcontext class 创建一个迁移配置 class。 - 当您输入
add-migration "migrationname"
时,将检查该 dbcontext class 是否存在差异。
因此,人们所要做的就是,与其自己制作源自 DbContext
的 class,不如使用那个。