C
C#•2mo ago
Tim

Issue with DbContext reading in DB Tables

I am trying to use DbContext and the optionsBuilder to connect to my database and obtain a table called "Card". I am doing the following command in
OnConfiguring
OnConfiguring
:
optionsBuilder.UseSqlite($"Data Source=mtg.db");
optionsBuilder.UseSqlite($"Data Source=mtg.db");
and also have the following object instantiated in the same class:
public DbSet<Card> Card { get; set; }
public DbSet<Card> Card { get; set; }
Then, in my main method I run the following lines:
var context = new MtgContext();
var dbCards = context.Card.ToList();
Console.WriteLine(dbCards);
var context = new MtgContext();
var dbCards = context.Card.ToList();
Console.WriteLine(dbCards);
I get an error that "Card" is not a known table even though I have a Card table in the database. What am I doing wrong?
71 Replies
Jimmacle
Jimmacle•2mo ago
you should be doing 1 of 2 things 1. create your dbcontext then use that to generate migrations to create your database 2. scaffold your dbcontext from your existing database otherwise you get issues like this where things aren't named what you expect
Tim
Tim•2mo ago
how do you generate migrations? right now my card table resembles my card class
Angius
Angius•2mo ago
Migrations Overview - EF Core
Overview of using migrations to manage database schemas with Entity Framework Core
Tim
Tim•2mo ago
Ah yeah, i've tried running
dotnet ef migrations add InitialCreate
dotnet ef migrations add InitialCreate
but it gives me this error: Could not execute because the specified command or file was not found. Possible reasons for this include: * You misspelled a built-in dotnet command. * You intended to execute a .NET program, but dotnet-ef does not exist. * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Angius
Angius•2mo ago
Did you install th dotnet-ef tool?
Tim
Tim•2mo ago
ah maybe not that fixed it
RedSatanFrog
RedSatanFrog•2mo ago
afterall, if you have just testing DB and no important data, you can use EnsureDeleted then EnsureCreated)
Tim
Tim•2mo ago
So now that I have the intiialcreate ran in the terminal do i have to manually add the tables with
dotnet ef migrations add [table]
dotnet ef migrations add [table]
?
Angius
Angius•2mo ago
No DbContext represents the database DbSet<T>s within represent the tables Each T represents the shape of the table
Tim
Tim•2mo ago
I see. so is there still a chance that it's not named card as i expected? it still doesnt seem to work
Angius
Angius•2mo ago
public class MyDbContext : DbContext
{
public required DbSet<User> Users { get; init; }
public required DbSet<Blogpost> Blogposts { get; init; }
}
public class MyDbContext : DbContext
{
public required DbSet<User> Users { get; init; }
public required DbSet<Blogpost> Blogposts { get; init; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public List<Blogpost> Blogposts { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public List<Blogpost> Blogposts { get; set; }
}
public class Blogpost
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public User Author { get; set; }
public int AuthorId { get; set; }
}
public class Blogpost
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public User Author { get; set; }
public int AuthorId { get; set; }
}
This will create a database with Users and Blogposts tables Blogpost having a foreign key to User Both having an auto-incremented integer primary key
Tim
Tim•2mo ago
Yeah
public class Card : ICard
{
[Key]
public int CardPK { get; set; }
public string CardName { get; set; }
public SpellTypes CardType { get; set; }
public string CardText { get; set; }
public int? Power { get; set; }
public int? Toughness { get; set; }
[NotMapped]
public Dictionary<LandTypes, int> ManaCost { get; set; }
[NotMapped]
public bool Tapped { get; set; }
[NotMapped]
public bool IsExiled { get; set; }
[NotMapped]
public bool IsInGraveyard { get; set; }
[NotMapped]
public int TurnCount { get; set; }
public class Card : ICard
{
[Key]
public int CardPK { get; set; }
public string CardName { get; set; }
public SpellTypes CardType { get; set; }
public string CardText { get; set; }
public int? Power { get; set; }
public int? Toughness { get; set; }
[NotMapped]
public Dictionary<LandTypes, int> ManaCost { get; set; }
[NotMapped]
public bool Tapped { get; set; }
[NotMapped]
public bool IsExiled { get; set; }
[NotMapped]
public bool IsInGraveyard { get; set; }
[NotMapped]
public int TurnCount { get; set; }
this is part of my card class, there are some methods as well but i didnt include them here And this things that are mapped are the same name in the database as well
Angius
Angius•2mo ago
[NotMapped] 💀 Get into the habit of using DTOs Database models should only describe the database table
Tim
Tim•2mo ago
Gotcha. I wasnt sure how else to represent this class because it requries the values that arent mapped and i wouldnt wanna represent those values in the database but with that aside, is there anythign else im missing? it doesnt seem like the migrations fixed it
Angius
Angius•2mo ago
Show your code and errors
Want results from more Discord servers?
Add your server