[英]Entity Framework 6: How do I reset a database using code first migrations and an FTP deploy
[英]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
我只會得到一個“空”遷移文件。 那我在做什么錯? 是否必須以某種方式引用我的DbContext? 實體框架只能處理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; }
}
}
“空”遷移文件:
namespace ReleaseDateMailer.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class addedwatchedgamemodel : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
}
“批量清潔”可能會解決您的問題。
這表明EF工具/程序集在默認生成輸出位置(通常是/ bin / Debug)之外的其他位置查找。 順便說一句,clean命令還清除中間輸出。
要進行批量清潔:
Build
-> Batch Build
Select All
Clean
關閉對話框,重建並重新嘗試遷移。
運行add-migration命令時,應將程序包管理器控制台指向具有DBContext類(WebApplication4.DataAccess)的項目。 如果要在與Web應用程序項目(假設為WebApplication4.Web)不同的項目中進行遷移,則應運行以下命令:
add-migration "MigrationName" -projectName:WebApplication.DataAccess -startupProjectName:WebApplication4.Web
希望能幫助到你!!
ApplicationDbContext
)! enable-migrations
,將基於找到的dbcontext類創建遷移配置類。 add-migration "migrationname"
,將檢查該dbcontext類是否存在差異。 因此,所有要做的就是,不要使用從DbContext
派生的自己的類, DbContext
使用該類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.