首页 > 解决方案 > Entity Framework Core 项目迁移失败,调试配置文件不存在

问题描述

我试图创建一个Entity Framework Core迁移,但它失败了。这是 Visual Studio 2019 包管理器控制台的输出

> Each package is licensed to you by its owner. NuGet is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies that are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies.
>
> Package Manager Console Host Version 5.7.0.6726
>
> Type 'get-help NuGet' to see all available NuGet commands.

PM> Add-Migration InitialCreate

> Build started...  
> Build failed.  

然后我尝试构建我的代码并遇到以下问题 debug profile does not exist:我该如何解决这个问题?我已经尝试了一切,但我似乎无法解决它。

标签: c#.netvisual-studioentity-frameworkwindows-10

解决方案


根据您的描述,您希望创建 Entity Framework Core 迁移。

我假设你使用 sqlserver。

您可以参考以下步骤来创建 EF Core 迁移。

首先,请创建一个控制台应用程序(.Net Core)。

其次,请安装以下nuget包:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools

第三,请在您的控制台应用程序中添加两个类。

public class Teacher
{
    public int TeaID { get; set; }
    public int Age { get; set; }
    public string Name { get; set; }
}

public class TeaContext : DbContext
{
    public DbSet<Teacher> Teachers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseSqlServer(@"Connectionstring");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Teacher>()
            .HasNoKey();
    }
}

Connectionstring 是你连接到sql server 数据库的字符串。(请先创建一个数据库) 第三,你可以在包管理器控制台中执行以下命令。

PM> Add-Migration Initial

PM> Update-Database

包管理器控制台

最后,您可以检查数据库设计。

数据库设计


推荐阅读