0

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.

marc_s
759k184 gold badges1.4k silver badges1.5k bronze badges
asked Feb 24, 2018 at 19:53

2 Answers 2

1

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.

answered Feb 24, 2018 at 20:01
1
  • 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. Commented Feb 25, 2018 at 12:28
0

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.

answered Feb 24, 2018 at 20:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.