【问题标题】:Dotnet core Entity Framework SQL data firstdotnet core Entity Framework SQL 数据优先
【发布时间】:2018-02-25 04:42:36
【问题描述】:

仔细遵循 Microsoft 教程说明(来自几个 dotnet 核心示例)。问题:我到了Add-Migration 步骤,它运行良好——除了在SQL Server 中,没有数据库出现。

所以我尝试预先创建一个空数据库;没有表格出现。从受信任的连接更改为用户 ID 和密码。还是没有运气。

我有 Visual Studio 2017 v15.5.7

步骤:新项目:Web Core 应用程序;这次选择了 WebAPI 选项。

Nuget:

  • Microsoft.EntityFrameworkCore.SqlServer (2.0.1)
  • Microsoft.EntityFrameworkCore.Tools (2.0.1)
  • Microsoft.VisualStudio.Web.CodeGeneration.Design (2.02)

Models.cs 包含:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace HunterDataCore.Models
{
    public class HunterContext : DbContext
    {
        public HunterContext(DbContextOptions<HunterContext> options)
            : base(options)
        { }

        public DbSet<Behavior> Behaviors { get; set; }
    }

    public class Behavior
    {
        [Key]
        public string BehaviorId { get; set; }
        public string ProjectKey { get; set; }
        [Required]
        [MaxLength(200)]
        public string Title { get; set; }
        [MaxLength(500)]
        public string Description { get; set; }
    }
}

Startup.cs .. 只改了一个函数:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    var connection = @"Server=Puppy;Database=HunterDataCore;Trusted_Connection=True;ConnectRetryCount=0";
    services.AddDbContext<HunterContext>(options => options.UseSqlServer(connection));
}

然后它说使用包管理控制台:

add-migration InitialCreate

它确实添加了一个 Migrations 文件夹以及 Initial Create: Up/Down/BuildTargetModel 函数。

UP功能:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
                name: "Behaviors",
                columns: table => new
                {
                    BehaviorId = table.Column<string>(nullable: false),
                    Description = table.Column<string>(maxLength: 500, nullable: true),
                    ProjectKey = table.Column<string>(nullable: true),
                    Title = table.Column<string>(maxLength: 200, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Behaviors", x => x.BehaviorId);
                });
}

没有运行,没有命中,没有错误,没有人留在基地 - 没有数据库,或者如果我预先添加它,数据库不包含任何新表。

有什么想法吗?

提前致谢。

【问题讨论】:

    标签: sql-server .net-core


    【解决方案1】:

    来自link,我想你正在使用它:

    Code First Migrations 有两个您将要执行的主要命令 熟悉。

    • Add-Migration 将根据您自上次迁移创建以来对模型所做的更改为下一次迁移搭建基架
    • Update-Database 会将所有挂起的迁移应用到数据库

    我将其解释为 Add-Migration 将在项目中创建代表要应用的迁移的文件,而 Update-Database 将应用此类迁移,因此您需要执行此类 Update-Database 才能实际创建/更新数据库。

    另外,您可以使用 -verbose 修饰符来查看是否发生了一些意想不到的事情。

    【讨论】:

    • 你是绝对正确的。我在添加迁移后停止阅读,认为我应该看到结果。我的错。非常感谢。
    【解决方案2】:

    您需要从命令行更新数据库。它不会在运行时自动应用迁移。

    dotnet ef database update

    它使用在您的代码中设置的数据库配置和连接。 IE。它实际上运行你的应用程序来实现这一点。

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2013-09-23
      • 2021-02-06
      • 2021-05-04
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多