C
C#5mo ago
Tiltyboi

dotnet-ef error creating migrations

Im following a youtube tutorial to create a discord bot and in a part the author connects to a database. (vid for reference: https://www.youtube.com/watch?v=EHmtrqb5VMk&list=PLS6sInD7ThM0Zb8F_KBl4T_jGF1e3apsc&index=11) (P.S excuse my cringe project name) When im calling the command from CMD its like this:
dotnet-ef migrations add InitialCreate -p ../SadBot.Migrations/SadBot.Migrations.csproj --context SadBot.Core.Database.RPGContext --startup-project ./SadMemerBot.csproj
dotnet-ef migrations add InitialCreate -p ../SadBot.Migrations/SadBot.Migrations.csproj --context SadBot.Core.Database.RPGContext --startup-project ./SadMemerBot.csproj
And then i get this error: No DbContext named 'RPGContext' was found. Or with full path No DbContext named 'SadBot.Core.Database.RPGContext' was found. Im following exactly and linking projects together, saving the csproj files and i can import my class in the program.cs so they are linked.. Why creating migrations doesnt work then? My Project strucutre is like so:
SadBot (solution)
-/SadBot.Core
-/Database
-/Entity.cs
-/RPGContext.cs
-/SadBot.Migrations
-/SadMemerBot (Startup proj)
-/Program.cs
SadBot (solution)
-/SadBot.Core
-/Database
-/Entity.cs
-/RPGContext.cs
-/SadBot.Migrations
-/SadMemerBot (Startup proj)
-/Program.cs
Dapper Dino
YouTube
How To Make A C# Discord Bot - Creating A Database - Part 10
Join Our Community: https://student.dapperdino.co.uk/ ----------------------------------------------------------------------------------------------------------- Just Here To Plug My Social Media Stuff: https://www.patreon.com/dapperdino https://www.twitch.tv/dapper_dino https://twitter.com/dapperdino4 https://github.com/DapperDino https://disco...
2 Replies
Pobiega
Pobiega5mo ago
Could you share the code for your RPGContext.cs file, and also Program.cs?
Tiltyboi
TiltyboiOP5mo ago
Program.cs
namespace SadMemerBot
{
public class Program
{
private DiscordSocketClient _client;

private Program()
{
_client = new DiscordSocketClient(); // commented out for abbrevity
}

public static Task Main(string[] args) => new Program().MainAsync(args);

public async Task MainAsync(string[] args)
{
var host = Host.CreateApplicationBuilder(args);


//Add any other services here
// services .....
var sqlDbConn = host.Configuration.GetConnectionString("SqlConnection");
host.Services.AddDbContext<RPGContext>(options =>
{
options.UseSqlServer(sqlDbConn, x => x.MigrationsAssembly("SadBot.Migrations"));
});

var runner = host.Build();
await runner.RunAsync();
}
namespace SadMemerBot
{
public class Program
{
private DiscordSocketClient _client;

private Program()
{
_client = new DiscordSocketClient(); // commented out for abbrevity
}

public static Task Main(string[] args) => new Program().MainAsync(args);

public async Task MainAsync(string[] args)
{
var host = Host.CreateApplicationBuilder(args);


//Add any other services here
// services .....
var sqlDbConn = host.Configuration.GetConnectionString("SqlConnection");
host.Services.AddDbContext<RPGContext>(options =>
{
options.UseSqlServer(sqlDbConn, x => x.MigrationsAssembly("SadBot.Migrations"));
});

var runner = host.Build();
await runner.RunAsync();
}
And then RPGContext:
using Microsoft.EntityFrameworkCore;
using SadBot.Core.Database.Models.Items;

namespace SadBot.Core.Database
{
public class RPGContext : DbContext
{
public RPGContext(DbContextOptions<RPGContext> options) : base(options) { }
public DbSet<Item> Items { get; set; }
}
}
using Microsoft.EntityFrameworkCore;
using SadBot.Core.Database.Models.Items;

namespace SadBot.Core.Database
{
public class RPGContext : DbContext
{
public RPGContext(DbContextOptions<RPGContext> options) : base(options) { }
public DbSet<Item> Items { get; set; }
}
}
Want results from more Discord servers?
Add your server