C
C#3y ago
Sal

EF Migration error

hi, I am getting an error message, and I am guessing comes from the connection string Initial catalog that I named after the name of the project from lack of info in all the sources I looked into. i followed the documentation and added App.config with a connection string and also read the page for ConfigurationManager but no one seems to tell how to name the "initial catalog" . i am following the dotNet tutorial on EF ,if anybody could help me out i really appreciate, thanks
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
ConfigurationManager.ConnectionStrings["myappnameDatabase"].ConnectionString);//line 19
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
ConfigurationManager.ConnectionStrings["myappnameDatabase"].ConnectionString);//line 19
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="myappnameDatabase"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=myappname;Integrated Security=True;" />
</connectionStrings>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="myappnameDatabase"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=myappname;Integrated Security=True;" />
</connectionStrings>
</configuration>
on package console
PM> Add-Migration InitialCreate
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
at myappname.Data.myappnameContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) (...) :line 19
PM> Add-Migration InitialCreate
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
at myappname.Data.myappnameContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) (...) :line 19
20 Replies
Angius
Angius3y ago
First time ever I see the connection string configured inside of some random XML file appsettings.json is usually where that config is
Sal
SalOP3y ago
Angius
Angius3y ago
Ah, a desktop project, that'd explain the XML
Sal
SalOP3y ago
do you see any flag?
Angius
Angius3y ago
Well, the error is about the connection string being empty. The only thing that comes to mind is either the XML file not being added to the configuration, or a name mismatch C# in general is case-sensitive, so connectionString property in that XML might be an issue Not sure
Sal
SalOP3y ago
i didnt add the App.config anywhere (other than it being in the solution) should i add it in App resources or anywhere else? its not, i try that, the solution build is fine.... no errors
Angius
Angius3y ago
Where is that XML you pasted if not in App.config..?
Sal
SalOP3y ago
my solution didnt have an app.config so i added, its side by side with app.xaml i meant reference if that was not clear
Angius
Angius3y ago
It should be in the root of your project, IIRC I'm honestly not sure
Sal
SalOP3y ago
it is
Angius
Angius3y ago
I never had the need to connect to a database from a desktop app
Sal
SalOP3y ago
how would you store data?
Angius
Angius3y ago
I generally don't make desktop apps, but I'd probably just use SQLite instead of LocalDB
Sal
SalOP3y ago
this is kind of breaking the flow.. just needed to store values locally
Angius
Angius3y ago
With SQLite, I wouldn't even use config, I'd just hardcode the connection string as "database.db" and call it a day
Sal
SalOP3y ago
well that would be an option i guess, just try to learn and they say to not hard code.. but either way am i not using sqll? that name is all over
Angius
Angius3y ago
With EF? No, you won't be writing any SQL code, you'd be using LINQ
Sal
SalOP3y ago
well yes but the data is stored in sql i had to get the nuget for sql from ef
Angius
Angius3y ago
SQL is a language The data would be stored in a database Whether that database is SQLIte, PostgreSQL, MariaDB, LiteDB, or SQL Server, that's another thing You probably had to get the nuget for SQL Server Since that's what LocalDB is based on
Sal
SalOP3y ago
ok, i think is sql server actualy will try to hardcode that string to the overide it works when hardcoded. weird, before get this "closed" if anybody knows what was wrong in the app.config or ConfigurationManager please let me know, thanks @Angius thanks for your time Anybody has an idea on how to fix this issue and not hardcode the connection string? thanks
Want results from more Discord servers?
Add your server