Carefully following Microsoft tutorial instructions (from a couple dotnet core examples). Problem: I get to the Add-Migration
step and it works perfectly - except that in SQL Server, no database shows up.
So I tried pre-creating an empty database; no tables show up. Changed from trusted connection to user id and password. Still no luck.
I have Visual Studio 2017 v15.5.7
Steps: new project: Web Core application; this time took WebAPI option.
Nuget:
- Microsoft.EntityFrameworkCore.SqlServer (2.0.1)
- Microsoft.EntityFrameworkCore.Tools (2.0.1)
- Microsoft.VisualStudio.Web.CodeGeneration.Design (2.02)
Models.cs contains:
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 .. only changed one function:
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));
}
Then it says using Package Manage Console:
add-migration InitialCreate
It indeed adds a Migrations
folder along with Initial Create: Up/Down/BuildTargetModel functions.
The UP function:
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);
});
}
No runs, no hits, no errors, no one left on base -- and NO database or if I pre-add it, the database does NOT contain any new tables.
Any thoughts?
Thanks in advance.
2 Answers 2
From this link, which is the one I suppose you are using:
Code First Migrations has two primary commands that you are going to become familiar with.
- Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
- Update-Database will apply any pending migrations to the database
I interpret it as that the Add-Migration would create the files in the project representing the migration to be applied, while the Update-Database will apply such migrations, so you need to execute such Update-Database to actually create/update the database.
Plus, you can use the -verbose modifier to both to see if there is some unexpected stuff happening.
-
You are absolutely right. I Stopped Reading after the add-migrations thinking that I should have seen the results. MY BAD. Thank you very much.Yogi Bear– Yogi Bear02/25/2018 12:28:03Commented Feb 25, 2018 at 12:28
You need to update the database from command line. It doesn't automatically apply the migrations when run.
dotnet ef database update
It uses the database configuration and connection that is set up in your code. I.e. it sort-of actually runs your app to achieve this.