I have successfully created a migration, but no matter what I do, I can't update the database and the problem seems to be in the connection string.
appsettings.json
:
{
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=LAPTOP-T70EVGQC\\MSSQLSERVER01;Initial Catalog=MoviesApi;Integrated Security=True;"
},
"frontend_url": "http://localhost:3000",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
program.cs
:
builder.Services.AddDbContext<ApplicationDBContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnections")));
ApplicationDbContext.cs
:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MoviesApi.Entities;
using System.Diagnostics.CodeAnalysis;
namespace MoviesApi
{
public class ApplicationDBContext: DbContext
{
public ApplicationDBContext([NotNullAttribute]DbContextOptions options) : base(options)
{
}
public DbSet<Genre> Genres { get; set; }
}
public class ApplicationDBContextFactory : IDesignTimeDbContextFactory<ApplicationDBContext>
{
public ApplicationDBContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDBContext>();
optionsBuilder.UseSqlServer("your connection string");
return new ApplicationDBContext(optionsBuilder.Options);
}
}
}
Migration files:
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MoviesApi.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Genres",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Genres", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Genres");
}
}
}
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MoviesApi;
#nullable disable
namespace MoviesApi.Migrations
{
[DbContext(typeof(ApplicationDBContext))]
partial class ApplicationDBContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("MoviesApi.Entities.Genre", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.HasKey("Id");
b.ToTable("Genres");
});
#pragma warning restore 612, 618
}
}
}
I'm trying to connect to the database. Using the Package Manager Console, I try to apply the migration (by running Update-Database
), but I get an error
Format of the initialization string does not conform to specification starting at index 0
Database should be updated with a table genres, if I'm not mistaken.
I even get the error just by getting the list of migrations with "get-migration"
Any help would be greatly appreciated.
Ok, i realized it was a dumb mistake.
Turns out that in the DBContext.cs there's this part
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDBContext>();
optionsBuilder.UseSqlServer("your connection string");
And that's where i should be passing the connection string. No wonder it didn't recognize it.