【发布时间】: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