C
C#3y ago
esther

Can someone help me? I got this error

Hi, so i have table Laporan_Penilaian form another database. When i tried to run my project i got this error. My suspect because failed connection to other database?
62 Replies
TheRanger
TheRanger3y ago
that just means it couldnt find a table with such name failed connection to database would throw a different error
esther
estherOP3y ago
Idk why it cant find, i mean the table name is right...
TheRanger
TheRanger3y ago
can you show the table?
esther
estherOP3y ago
sure
esther
estherOP3y ago
this
TheRanger
TheRanger3y ago
thats just a sql command, did u even run it? does the table even exist?
esther
estherOP3y ago
the tbale exist table*
TheRanger
TheRanger3y ago
can u show where it exists?
esther
estherOP3y ago
this is the table, sorry only can show this there's sensitive data
TheRanger
TheRanger3y ago
maybe ur connecting to the wrong database
esther
estherOP3y ago
Is that affect if i have table Laporan_Penilaian but i named my model LaporanPenilaian? I connected to the right database
esther
estherOP3y ago
This is my connection string. Default for db1 and log for db2 (the table laporan_penilain exist). Maybe am i wrong here?
TheRanger
TheRanger3y ago
show code where u are trying to access the table
esther
estherOP3y ago
this one(?)
TheRanger
TheRanger3y ago
anyway i g2g, check the connection string
esther
estherOP3y ago
okay
Kouhai
Kouhai3y ago
Are you sure you're connection to both dbs?
esther
estherOP3y ago
I’m not sure actually I’m connecting to db2 Kuo you’re catpog
Kouhai
Kouhai3y ago
How are you connecting to the databases catthinking
esther
estherOP3y ago
I mean i write my server and database name here.
Kouhai
Kouhai3y ago
Yeah, these are your connection string, how are you using EF to do the actual connection using these strings?
esther
estherOP3y ago
Emm i followed the tutorial. Not sure how to connect double database in EF. Db1 succeed connect to EF.
Kouhai
Kouhai3y ago
Okay, in ConfigureServices how are you adding DbContext?
esther
estherOP3y ago
This is my RepositoryContext
Kouhai
Kouhai3y ago
Do you have a call like this services.AddDbContext<T>(....) in ConfigureServices in Startup.cs ?
Tvde1
Tvde13y ago
You have to create a 2nd context probably to connect to a 2nd db
esther
estherOP3y ago
No i can't find this
Tvde1
Tvde13y ago
I wouldn't advise mixing them into one context
esther
estherOP3y ago
Ah so you mean 2 repositorycontext?
Tvde1
Tvde13y ago
Control + Shift + F and look for AddDbContext Why is your data in two different databases?
esther
estherOP3y ago
It's default from there Okay i will try to create 2nd context, thanks
Kouhai
Kouhai3y ago
You'll need to add the other context using AddDbContext
esther
estherOP3y ago
How to this?😅 I look for Ctrl + shift + AddDbContext but i don't understand
Kouhai
Kouhai3y ago
What guide did you use for EF, thinkinggura ?
Kouhai
Kouhai3y ago
Press Ctrl + Shift + F and select Entire solution from the dropdown menu
Kouhai
Kouhai3y ago
and search AddDbContext
esther
estherOP3y ago
esther
estherOP3y ago
esther
estherOP3y ago
this one, yes?
Tvde1
Tvde13y ago
yes you want to duplicate this for your new context you probably want to name the contexts a bit better so it's clear which data is where for example ProductContext and CustomerContext if the data is in different DBs but is there no way you can merge the data in one db? Makes it a lot easier
Kouhai
Kouhai3y ago
Adding to Tvde1's answer, make sure to change the connection string
Kouhai
Kouhai3y ago
for you new context
esther
estherOP3y ago
Okay i already duplicate that and change the name connection string, what's next? No it's impossible to merger data in one db cause it's actually the database already running in another project too
Kouhai
Kouhai3y ago
You consume that context like you'd normally do blobthumbsup
esther
estherOP3y ago
And i added new dbcontext file, but it still has same error 😅
Kouhai
Kouhai3y ago
Did you change the type when duplicating AddDbContext?
esther
estherOP3y ago
yes Kou i change it to MappiEgovContext too Wait maybe i should try to the default one. Only change this one
Kouhai
Kouhai3y ago
Yeah, make sure you have changed all instances
esther
estherOP3y ago
This is right, right? But i still get same error 😅
Tvde1
Tvde13y ago
is Cache:Enable true? Then you also have to add a DbContextPool for the new context
Kouhai
Kouhai3y ago
The type is wrong 😅
Kouhai
Kouhai3y ago
it should be MappiEgovContext
Tvde1
Tvde13y ago
ah yes that too
esther
estherOP3y ago
OMG i missed catlove
Kouhai
Kouhai3y ago
blobthumbsup
esther
estherOP3y ago
It's not working😵 Should i change this to MappiEgovContext? But if i changed it, repositoryContext error
Tvde1
Tvde13y ago
yes you should change it there what is the error you get?
esther
estherOP3y ago
this
Tvde1
Tvde13y ago
your repository base needs to be updated :) maybe add a generic type of the repository itself
esther
estherOP3y ago
Can i just duplicate this?
Tvde1
Tvde13y ago
I would make it
public abstract class RepositoryBase<TEntity, TContext> : IRepositoryBase<TEntity, TContext>
where TEntity : class
where TContext : DbContext
{
protected TContext RepositoryContext { get; set; }
}
public abstract class RepositoryBase<TEntity, TContext> : IRepositoryBase<TEntity, TContext>
where TEntity : class
where TContext : DbContext
{
protected TContext RepositoryContext { get; set; }
}
but this is best for you to figure out how to best add this into your software
esther
estherOP3y ago
Hey thank you so much finally i can connect to my db2. i never expected i need to change so many file too. Thanks again😊
Want results from more Discord servers?
Add your server