C
C#•4mo 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•4mo 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
TimOP•4mo ago
how do you generate migrations? right now my card table resembles my card class
Angius
Angius•4mo ago
Migrations Overview - EF Core
Overview of using migrations to manage database schemas with Entity Framework Core
Tim
TimOP•4mo 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•4mo ago
Did you install th dotnet-ef tool?
Tim
TimOP•4mo ago
ah maybe not that fixed it
RedSatanFrog
RedSatanFrog•4mo ago
afterall, if you have just testing DB and no important data, you can use EnsureDeleted then EnsureCreated)
Tim
TimOP•4mo 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•4mo ago
No DbContext represents the database DbSet<T>s within represent the tables Each T represents the shape of the table
Tim
TimOP•4mo 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•4mo 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
TimOP•4mo 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•4mo ago
[NotMapped] 💀 Get into the habit of using DTOs Database models should only describe the database table
Tim
TimOP•4mo 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•4mo ago
Show your code and errors
Tim
TimOP•4mo ago
using MTG.Database;

namespace MTG.Gameplay
{
public class Program
{
public static void Main(string[] args)
{
var context = new MtgContext();
var dbCards = context.Card.ToList();
Console.WriteLine(dbCards);
}
}

}
using MTG.Database;

namespace MTG.Gameplay
{
public class Program
{
public static void Main(string[] args)
{
var context = new MtgContext();
var dbCards = context.Card.ToList();
Console.WriteLine(dbCards);
}
}

}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MTG.Classes;
using MTG.Enums;
using MTG.Gameplay;

using MTG.Players;

namespace MTG.Database
{
public class MtgContext : DbContext
{
public DbSet<Card> Card { get; set; }

//public DbSet<AbilitiesLK> Abilities {get; set;}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

optionsBuilder.UseSqlite($"Data Source=mtg.db)");
}
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MTG.Classes;
using MTG.Enums;
using MTG.Gameplay;

using MTG.Players;

namespace MTG.Database
{
public class MtgContext : DbContext
{
public DbSet<Card> Card { get; set; }

//public DbSet<AbilitiesLK> Abilities {get; set;}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

optionsBuilder.UseSqlite($"Data Source=mtg.db)");
}
}
}
using MTG.Enums;
using MTG.Interfaces;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace MTG.Players
{
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 virtual void EnterBattlefield(bool tapped)
{
IsInGraveyard = false;
}

public virtual void EnterGraveyard()
{
IsInGraveyard = true;
}

public virtual void Exile()
{
IsExiled = true;
}

public void ToggleTapped()
{
Tapped = !Tapped;
}
}
}
using MTG.Enums;
using MTG.Interfaces;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace MTG.Players
{
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 virtual void EnterBattlefield(bool tapped)
{
IsInGraveyard = false;
}

public virtual void EnterGraveyard()
{
IsInGraveyard = true;
}

public virtual void Exile()
{
IsExiled = true;
}

public void ToggleTapped()
{
Tapped = !Tapped;
}
}
}
thats all the relevant code i beleive
Angius
Angius•4mo ago
And what's the error?
Tim
TimOP•4mo ago
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'no such table: Card'.
Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'no such table: Card'.
Angius
Angius•4mo ago
What do migrations say?
Tim
TimOP•4mo ago
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MTG.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Card",
columns: table => new
{
CardPK = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
CardName = table.Column<string>(type: "TEXT", nullable: false),
CardType = table.Column<int>(type: "INTEGER", nullable: false),
CardText = table.Column<string>(type: "TEXT", nullable: false),
Power = table.Column<int>(type: "INTEGER", nullable: true),
Toughness = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Card", x => x.CardPK);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Card");
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MTG.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Card",
columns: table => new
{
CardPK = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
CardName = table.Column<string>(type: "TEXT", nullable: false),
CardType = table.Column<int>(type: "INTEGER", nullable: false),
CardText = table.Column<string>(type: "TEXT", nullable: false),
Power = table.Column<int>(type: "INTEGER", nullable: true),
Toughness = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Card", x => x.CardPK);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Card");
}
}
}
// <auto-generated />
using System;
using MTG.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace MTG.Migrations
{
[DbContext(typeof(MtgContext))]
partial class MtgContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");

modelBuilder.Entity("MTG.Players.Card", b =>
{
b.Property<int>("CardPK")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<string>("CardName")
.IsRequired()
.HasColumnType("TEXT");

b.Property<string>("CardText")
.IsRequired()
.HasColumnType("TEXT");

b.Property<int>("CardType")
.HasColumnType("INTEGER");

b.Property<int?>("Power")
.HasColumnType("INTEGER");

b.Property<int?>("Toughness")
.HasColumnType("INTEGER");

b.HasKey("CardPK");

b.ToTable("Card");
});
#pragma warning restore 612, 618
}
}
}
// <auto-generated />
using System;
using MTG.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace MTG.Migrations
{
[DbContext(typeof(MtgContext))]
partial class MtgContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");

modelBuilder.Entity("MTG.Players.Card", b =>
{
b.Property<int>("CardPK")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<string>("CardName")
.IsRequired()
.HasColumnType("TEXT");

b.Property<string>("CardText")
.IsRequired()
.HasColumnType("TEXT");

b.Property<int>("CardType")
.HasColumnType("INTEGER");

b.Property<int?>("Power")
.HasColumnType("INTEGER");

b.Property<int?>("Toughness")
.HasColumnType("INTEGER");

b.HasKey("CardPK");

b.ToTable("Card");
});
#pragma warning restore 612, 618
}
}
}
Angius
Angius•4mo ago
No, the cli when you migrate
Tim
TimOP•4mo ago
dotnet ef migrations add InitialCreate
>>
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
dotnet ef migrations add InitialCreate
>>
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
dotnet ef database update
Build started...
Build succeeded.
Applying migration '20240720203616_InitialCreate'.
Done.
dotnet ef database update
Build started...
Build succeeded.
Applying migration '20240720203616_InitialCreate'.
Done.
Angius
Angius•4mo ago
And when you apply that migration huh
Tim
TimOP•4mo ago
those are the only two commands i have done
Angius
Angius•4mo ago
Huh
Tim
TimOP•4mo ago
D: any other suggestions to try? i feel like its a really silly mistake but im not sure what im overlooking
Angius
Angius•4mo ago
Delete mtg.db file, delete the migrations, create the migration and apply it again Only thing that comes to mind
Tim
TimOP•4mo ago
do i delete the .dll and exe of it too? there ar a few diufferent file types of the same name
Angius
Angius•4mo ago
What dll and exe...?
Angius
Angius•4mo ago
Where is that?
Tim
TimOP•4mo ago
net8.0 folder
C:\Users\timo2\OneDrive\Desktop\The Grind\Personal Projects\MTG\MTG\MTG\bin\Debug\net8.0
C:\Users\timo2\OneDrive\Desktop\The Grind\Personal Projects\MTG\MTG\MTG\bin\Debug\net8.0
is there another place to delete it
Angius
Angius•4mo ago
Right... you know what, just delete the bin and obj folders altogether It's safe to do, no worries
Tim
TimOP•4mo ago
kk and now do dotnet ef migrations remove ?
Angius
Angius•4mo ago
Nah, just remove the Migrations folder
Tim
TimOP•4mo ago
okie and re run ? or just run those two migration commands and then re run program
Angius
Angius•4mo ago
yeah
Tim
TimOP•4mo ago
got a diff output when i did update now hm
Angius
Angius•4mo ago
Oh?
Tim
TimOP•4mo ago
Build succeeded.
Applying migration '20240720210319_InitialCreate'.
Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "Card" (
"CardPK" INTEGER NOT NULL CONSTRAINT "PK_Card" PRIMARY KEY AUTOINCREMENT,
"CardName" TEXT NOT NULL,
"CardType" INTEGER NOT NULL,
"CardText" TEXT NOT NULL,
"Power" INTEGER NULL,
"Toughness" INTEGER NULL
);
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'table "Card" already exists'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements()+MoveNext()
at Microsoft.Data.Sqlite.SqliteCommand.GetStatements()+MoveNext()
at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at
Build succeeded.
Applying migration '20240720210319_InitialCreate'.
Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "Card" (
"CardPK" INTEGER NOT NULL CONSTRAINT "PK_Card" PRIMARY KEY AUTOINCREMENT,
"CardName" TEXT NOT NULL,
"CardType" INTEGER NOT NULL,
"CardText" TEXT NOT NULL,
"Power" INTEGER NULL,
"Toughness" INTEGER NULL
);
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'table "Card" already exists'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements()+MoveNext()
at Microsoft.Data.Sqlite.SqliteCommand.GetStatements()+MoveNext()
at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at
Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
SQLite Error 1: 'table "Card" already exists'.
Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
SQLite Error 1: 'table "Card" already exists'.
but the program still doesnt work 😭
Angius
Angius•4mo ago
Huh I... think I know what might be happening here
Tim
TimOP•4mo ago
ill be back on later but if you guys have any suggestiuons that would be helpful
Jimmacle
Jimmacle•4mo ago
applying migrations to the wrong db? :LUL:
Angius
Angius•4mo ago
It's creating a new database file with each build
Jimmacle
Jimmacle•4mo ago
the build itself shouldn't, but running the migrations would create one wherever the connection string says to
Angius
Angius•4mo ago
I wonder if creating mtg.db in the project root and copying it on build would fix it
Tim
TimOP•4mo ago
wdym copying it on build? i access the db in ssms
Jimmacle
Jimmacle•4mo ago
ssms works with sqlite? are you 100% sure that the file you're applying migrations to and the one your program is opening are the same?
Angius
Angius•4mo ago
See, that's why I prefer running Pg even for small projects like this, no need to worry about which file I open, which gets used, which gets created, and whether all three of those are the same file or not :KEKW:
Jimmacle
Jimmacle•4mo ago
sqlite also has some quirks with write-ahead logging that can store data in a file other than the main .db file and cause more confusion
Tim
TimOP•4mo ago
i am not sure ...😂 do yall know what companies with big databases in ssms usually use to connect to c# if not dbcontext?
Jimmacle
Jimmacle•4mo ago
that's not the problem here, EF core is a very common and well supported library ssms isn't a database, it's a tool to manage databases in your case your actual database is sqlite
Tim
TimOP•4mo ago
ah i see what u mean Well i created the database in ssms. does it make it sqlite by default?
Jimmacle
Jimmacle•4mo ago
i have no idea, ssms is primarily used to manage MS SQL Server instances you shouldn't be creating the database in ssms at all if you're using migrations
Tim
TimOP•4mo ago
oof
Jimmacle
Jimmacle•4mo ago
it will be created when you apply the migrations
Tim
TimOP•4mo ago
so how would i instead connect it to the one made in ssms
Jimmacle
Jimmacle•4mo ago
and since sqlite works with files and not a connection to a server, you have to make sure the file you're migrating and the file your program is using at runtime are the same file
Tim
TimOP•4mo ago
or how would i make a new one that's still accessible in ssms i suppose gotcha How would I verify that?
Jimmacle
Jimmacle•4mo ago
check the working directory of your program and make sure that the db file in that folder is the one you're migrating or opening in ssms afaik sqlite also does not like multiple programs opening the same db at once, so i'd avoid doing that
Tim
TimOP•4mo ago
how would ik what i'm migrating tho ?
Jimmacle
Jimmacle•4mo ago
it uses the same connection string that you put in OnConfiguring
Tim
TimOP•4mo ago
ah yeah
Jimmacle
Jimmacle•4mo ago
and that file will be relative to the working directory of the program, which is probably the folder where the exe is but it can also be the project folder
Tim
TimOP•4mo ago
i'm pretty sure it's not the same as the one in sssms since i created the db in sssms but i'm not sure how to point it to that one
Jimmacle
Jimmacle•4mo ago
honestly unless you need sqlite specifically i'd skip the headache and install a proper database server like sql express sqlite databases are just files, you'd point it by opening the database that you want to use
Tim
TimOP•4mo ago
gotcha, will look into that when i get home so do i have to re create the database using sql express? or how would this work
Jimmacle
Jimmacle•4mo ago
you'll need to recreate the migrations for sql server instead of sqlite, that's about it then use a connection string for your sql express instance instead of a sqlite database file
Tim
TimOP•4mo ago
how do you point the migrations at the sql server tho ? do u just change the cmd in the cli
Jimmacle
Jimmacle•4mo ago
by changing the ef core provider library and connection string so instead of .UseSqlite(...) it would be .UseSqlServer(...)
Tim
TimOP•4mo ago
ohhh gotcha I blame chat gpt lmao I'll try that out soon and lyk how it goes alright well im getting a diff error now at least:
Microsoft.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'
Microsoft.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'
on ssms im connecting through localhost tho, i wasnt able to get my other server to work im using this instead
optionsBuilder.UseSqlServer(@"Server=localhost;Database=mtg;Trusted_Connection=True;");
optionsBuilder.UseSqlServer(@"Server=localhost;Database=mtg;Trusted_Connection=True;");
but now i get this issue:
Microsoft.Data.SqlClient.SqlException: 'A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)'
Microsoft.Data.SqlClient.SqlException: 'A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)'
I think I have it working now (ik its bad practice but i ended up adding "Encrypt=False"). Thanks so much for your help guys !
Want results from more Discord servers?
Add your server