标签: ef-migrations

EF迁移:回滚上次应用的迁移?

这看起来像一个非常常见的任务,但我找不到一个简单的方法来做到这一点.

我想撤消上次应用的迁移.我本来期待一个简单的命令,比如

PM> Update-Database -TargetMigration:"-1"
Run Code Online (Sandbox Code Playgroud)

相反,我能想出的就是:

PM> Get-Migrations

Retrieving migrations that have been applied to the target database.
201208012131302_Add-SystemCategory
201207311827468_CategoryIdIsLong
201207232247409_AutomaticMigration
201207211340509_AutomaticMigration
201207200025294_InitialCreate

PM> Update-Database -TargetMigration:"CategoryIdIsLong"
Run Code Online (Sandbox Code Playgroud)

(至少我可以只使用名称,跳过时间戳......)

有没有更简单的方法?

entity-framework database-migration entity-framework-4 ef-migrations

406
推荐指数
9
解决办法
27万
查看次数

重置实体框架迁移

我已经搞砸了我的迁移,我用于IgnoreChanges初始迁移,但现在我想要删除所有迁移,并开始使用所有逻辑进行初始迁移.

当我删除文件夹中的迁移并尝试Add-Migration它并不生成一个完整的文件(它是空的 - 因为我上次没有做任何更改,但现在已删除,迁移).

是否有任何Disable-Migrations命令,所以我可以重新运行Enable-Migrations

entity-framework database-migration ef-migrations

287
推荐指数
8
解决办法
18万
查看次数

实体框架 - 重新开始 - 撤消/回滚所有迁移

出于某种原因,我的迁移似乎是混乱/损坏/无论如何.我只是想重新开始,所以有没有办法彻底撤消所有迁移,删除历史记录,删除迁移代码,所以我回到原点?

例如)PM> Disable-MigrationsRollback-Migrations

我不想"更新"到原始的迁移步骤(比如InitialSchema目标),因为我再也找不到它了.

entity-framework ef-migrations entity-framework-4.3

167
推荐指数
4
解决办法
8万
查看次数

从EF 5 Code First Migrations生成完整的SQL脚本

如何使用Entity Framework 5 Code First Migrations创建从初始(空)状态到最新迁移的完整数据库脚本?

博客文章在MSDN博客暗示要做到这一点,但它似乎创建一个空的脚本:

Update-Database -Script -SourceMigration: $InitialDatabase
Run Code Online (Sandbox Code Playgroud)

entity-framework ef-migrations

136
推荐指数
3
解决办法
9万
查看次数

调试代码优先的Entity Framework迁移代码

我首先在我的网站上使用实体框架代码,我只是想知道是否有任何方法来调试迁移代码.你知道,比如设置断点和类似的东西.

我正在使用Package Manager Console使用update-database更新数据库.

谢谢

entity-framework ef-code-first ef-migrations entity-framework-5

131
推荐指数
5
解决办法
4万
查看次数

术语"更新 - 数据库"未被识别为cmdlet的名称

我正在使用EF5 beta1,而我之前能够运行"更新数据库".现在我关闭了Visual Studio,我无法让它运行.我收到以下错误:

术语"更新 - 数据库"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.在行:1 char:16 + Update-Database <<<< -verbose + CategoryInfo:ObjectNotFound:(Update-Database:String)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException

我试图重新安装EF5b1并且虽然成功(已安装),但"更新数据库"仍然无效.

谁能帮忙???

entity-framework ef-code-first ef-migrations entity-framework-5

104
推荐指数
9
解决办法
8万
查看次数

如何使用EF 4.3中的复杂密钥使用AddOrUpdate对数据进行种子设定

我正在尝试使用一些测试数据来开发数据库.

context.People.AddOrUpdate(p => p.Id, people));用得很成功了.

我有另一个我需要播种的表,其中我不知道主键.

例如,我想基于First和Last名称匹配AddOrUpdate.

我不确定如何正确编写表达式.

context.People.AddOrUpdate(p => p.FirstName && p.LastName, people);
Run Code Online (Sandbox Code Playgroud)

显然不正确,但我希望它能传达我正在寻找的解决方案.

c# seed linq-expressions ef-migrations entity-framework-4.3

101
推荐指数
1
解决办法
3万
查看次数

实体框架迁移重命名表和列

我重命名了几个实体及其导航属性,并在EF 5中生成了一个新的迁移.通常在EF迁移中重命名,默认情况下它会删除对象并重新创建它们.这不是我想要的,所以我几乎不得不从头开始构建迁移文件.

    public override void Up()
    {
        DropForeignKey("dbo.ReportSectionGroups", "Report_Id", "dbo.Reports");
        DropForeignKey("dbo.ReportSections", "Group_Id", "dbo.ReportSectionGroups");
        DropForeignKey("dbo.Editables", "Section_Id", "dbo.ReportSections");
        DropIndex("dbo.ReportSectionGroups", new[] { "Report_Id" });
        DropIndex("dbo.ReportSections", new[] { "Group_Id" });
        DropIndex("dbo.Editables", new[] { "Section_Id" });

        RenameTable("dbo.ReportSections", "dbo.ReportPages");
        RenameTable("dbo.ReportSectionGroups", "dbo.ReportSections");
        RenameColumn("dbo.ReportPages", "Group_Id", "Section_Id");

        AddForeignKey("dbo.ReportSections", "Report_Id", "dbo.Reports", "Id");
        AddForeignKey("dbo.ReportPages", "Section_Id", "dbo.ReportSections", "Id");
        AddForeignKey("dbo.Editables", "Page_Id", "dbo.ReportPages", "Id");
        CreateIndex("dbo.ReportSections", "Report_Id");
        CreateIndex("dbo.ReportPages", "Section_Id");
        CreateIndex("dbo.Editables", "Page_Id");
    }

    public override void Down()
    {
        DropIndex("dbo.Editables", "Page_Id");
        DropIndex("dbo.ReportPages", "Section_Id");
        DropIndex("dbo.ReportSections", "Report_Id");
        DropForeignKey("dbo.Editables", "Page_Id", "dbo.ReportPages");
        DropForeignKey("dbo.ReportPages", "Section_Id", "dbo.ReportSections");
        DropForeignKey("dbo.ReportSections", "Report_Id", "dbo.Reports");

        RenameColumn("dbo.ReportPages", "Section_Id", "Group_Id"); …
Run Code Online (Sandbox Code Playgroud)

c# sql-server entity-framework ef-migrations entity-framework-5

101
推荐指数
4
解决办法
8万
查看次数

数据库中已经有一个对象

程序包管理器控制台中的Update-Database失败.我使用了Entity Framework 6.x和代码优先方法.错误是

"数据库中已经有一个名为'AboutUs'的对象."

我怎么解决这个问题?

internal sealed class Configuration 
    : DbMigrationsConfiguration<Jahan.Blog.Web.Mvc.Models.JahanBlogDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = false;
    }

    protected override void Seed(Jahan.Blog.Web.Mvc.Models.JahanBlogDbContext context)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

我的DbContext是:

public class JahanBlogDbContext : IdentityDbContext<User, Role, int, UserLogin, UserRole, UserClaim>
{
    public JahanBlogDbContext()
        : base("name=JahanBlogDbConnectionString")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<JahanBlogDbContext>());
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Comment>().HasRequired(t => t.Article).WithMany(t => t.Comments).HasForeignKey(d => d.ArticleId).WillCascadeOnDelete(true);
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<UserRole>().ToTable("UserRole");
        modelBuilder.Entity<UserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<UserClaim>().ToTable("UserClaim");
    }

    public virtual DbSet<Article> Articles { get; set; }
    public …
Run Code Online (Sandbox Code Playgroud)

database entity-framework ef-code-first ef-migrations entity-framework-6

100
推荐指数
7
解决办法
15万
查看次数

MVC 5种子用户和角色

我一直在玩新的MVC 5,我有一些模型,控制器和视图设置使用代码优先迁移.

我的问题是如何为用户和角色提供种子?我目前在Configuration.cs中的Seed方法中播放一些参考数据.但是在我看来,在第一次点击AccountController之前,不会创建用户和角色表.

我目前有两个连接字符串,所以我可以将我的数据从我的身份验证分成不同的数据库.

如何让用户,角色等表格与我的其他人一起填充?而不是当帐户控制器被击中?

asp.net-mvc seeding ef-migrations asp.net-mvc-5

91
推荐指数
4
解决办法
6万
查看次数