相关疑难解决方法(0)

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

我重命名了几个实体及其导航属性,并在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万
查看次数

"无法找到对象"dbo.xxxx"因为它不存在或您没有权限."

在我的MVC Web应用程序,我添加了一个名为model 竞争力,以现有的模式,并没有add-migrationupdate-database,它工作得很好,然后我创建了基于该模型的控制,这是确定.

但在那之后,我意识到出了问题,所以我用模型删除了控制器及其视图.我也从Server Explorer中删除了表,并创建了一个名为CompetencyLabel的新模型,然后运行add-migration,它工作正常,但是当我运行时update-database,我在PM> window中遇到了这个错误:

"找不到对象"dbo.Competencies",因为它不存在或者您没有权限."

该表已被删除,我不知道为什么它仍然会问我.有人知道如何使它工作吗?非常感谢.

ef-migrations

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

在不丢弃数据的情况下重命名Entity Framework Core中的外键

我有两个模型类:

public class Survey
{
    public int SurveyId { get; set; }
    public string Name { get; set; }
}

public class User
{
    public int UserId { get; set; }

    public int SurveyId { get; set; }
    public Survey Survey { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我要重命名SurveyStudentSurvey,所以就会产生StudentSurveyId.我相应地更新了模型中的类名和属性,并添加了一个迁移.

但是,我得到:

ALTER TABLE语句与FOREIGN KEY约束"FK_User_Surveys_SurveyId"冲突.冲突发生在数据库"AppName",表"dbo.Survey",列"SurveyId"中.

我认为它试图删除数据,因为它需要列中的数据(不能为空)我看到了这个错误.但我不想丢弃数据.我怎样才能重命名呢?

c# sql-server entity-framework entity-framework-core ef-core-2.0

5
推荐指数
1
解决办法
3059
查看次数

使用Entity Framework Core 2.0更改或重命名列名而不会丢失数据

我意识到自己拼写的列标题中的一个不正确,因此我在模型中进行了更改,并创建了一个新迁移以将其更新到数据库中。在我意识到实际上似乎发生的事情是新的列替换了现有的列并擦除了所有数据之前,所有方法都运行良好。碰巧的是,由于这是一个教程数据库,将数据放回原处就没什么大不了,只需花费几分钟。

如何/如何更新/重命名列而不丢失其中的数据?

不确定在我的搜索中是怎么出现的,但这是直接相关的文章: 使用自动迁移,重命名表字段而不会丢失数据

c# entity-framework ef-migrations .net-core

4
推荐指数
2
解决办法
2227
查看次数