[英]How do I get Entity Framework Code First Migrations to see my models?
I made a new ASP.Net Web Application and enabled migrations on it. 我制作了一个新的ASP.Net Web应用程序,并在其上启用了迁移。 I ran
add-migration initial
and the initial
migration does in fact have all the default tables for authentication (dbo.AspNetRoles, dbo.AspNetUserRoles, etc). 我运行了
add-migration initial
,实际上initial
迁移确实具有用于身份验证的所有默认表(dbo.AspNetRoles,dbo.AspNetUserRoles等)。 However, when I create my own context and add an entity model to it, I can't get migrations to acknowledge that model. 但是,当我创建自己的上下文并向其中添加实体模型时,我无法进行迁移以确认该模型。 That is, when I run
add-migration added-watchedgame-model
I just get an "empty" migration file. 也就是说,当我运行
add-migration added-watchedgame-model
我只会得到一个“空”迁移文件。 So what am I doing wrong? 那我在做什么错? Does my DbContext have to be referenced somehow?
是否必须以某种方式引用我的DbContext? can Entity Framework only handle migrations for 1 dbcontext?
实体框架只能处理1个dbcontext的迁移吗?
ReleaseDateMailerDBContext.cs: 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: 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" migration file: “空”迁移文件:
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" may resolve your porblem. “批量清洁”可能会解决您的问题。
It suggests that the EF tooling/assemblies are looking in a location other than the default build output location (typically /bin/Debug). 这表明EF工具/程序集在默认生成输出位置(通常是/ bin / Debug)之外的其他位置查找。 The clean command also, incidentally, clears intermediary outputs.
顺便说一句,clean命令还清除中间输出。
To do a batch clean: 要进行批量清洁:
Build
-> Batch Build
Build
-> Batch Build
Select All
Select All
Clean
Clean
Close dialog, rebuild and re-attempt migration. 关闭对话框,重建并重新尝试迁移。
While running the add-migration command your package manager console should be pointed to the project having your DBContext class (WebApplication4.DataAccess). 运行add-migration命令时,应将程序包管理器控制台指向具有DBContext类(WebApplication4.DataAccess)的项目。 If you have migration in a different project than your web application project (suppose WebApplication4.Web) then you should run the following command:
如果要在与Web应用程序项目(假设为WebApplication4.Web)不同的项目中进行迁移,则应运行以下命令:
add-migration "MigrationName" -projectName:WebApplication.DataAccess -startupProjectName:WebApplication4.Web
Hope it helps!! 希望能帮助到你!!
ApplicationDbContext
) is already created! ApplicationDbContext
)! enable-migrations
, a migration configuration class is created based on the dbcontext class that it finds. enable-migrations
,将基于找到的dbcontext类创建迁移配置类。 add-migration "migrationname"
, That dbcontext class is what is checked for differences. add-migration "migrationname"
,将检查该dbcontext类是否存在差异。 So all one has to do is, rather than making one's own class that derives from DbContext
, use that one. 因此,所有要做的就是,不要使用从
DbContext
派生的自己的类, DbContext
使用该类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.