C
C#2y ago
daisy

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
TheRanger2y ago
that just means it couldnt find a table with such name failed connection to database would throw a different error
daisy
daisy2y ago
Idk why it cant find, i mean the table name is right...
TheRanger
TheRanger2y ago
can you show the table?
daisy
daisy2y ago
sure
daisy
daisy2y ago
this
TheRanger
TheRanger2y ago
thats just a sql command, did u even run it? does the table even exist?
daisy
daisy2y ago
the tbale exist table*
TheRanger
TheRanger2y ago
can u show where it exists?
daisy
daisy2y ago
this is the table, sorry only can show this there's sensitive data
TheRanger
TheRanger2y ago
maybe ur connecting to the wrong database
daisy
daisy2y ago
Is that affect if i have table Laporan_Penilaian but i named my model LaporanPenilaian? I connected to the right database
daisy
daisy2y ago
This is my connection string. Default for db1 and log for db2 (the table laporan_penilain exist). Maybe am i wrong here?
TheRanger
TheRanger2y ago
show code where u are trying to access the table
daisy
daisy2y ago
this one(?)
TheRanger
TheRanger2y ago
anyway i g2g, check the connection string
daisy
daisy2y ago
okay
Kouhai
Kouhai2y ago
Are you sure you're connection to both dbs?
daisy
daisy2y ago
I’m not sure actually I’m connecting to db2 Kuo you’re catpog
Kouhai
Kouhai2y ago
How are you connecting to the databases catthinking
daisy
daisy2y ago
I mean i write my server and database name here.
Kouhai
Kouhai2y ago
Yeah, these are your connection string, how are you using EF to do the actual connection using these strings?
daisy
daisy2y ago
Emm i followed the tutorial. Not sure how to connect double database in EF. Db1 succeed connect to EF.
Kouhai
Kouhai2y ago
Okay, in ConfigureServices how are you adding DbContext?
daisy
daisy2y ago
This is my RepositoryContext
Kouhai
Kouhai2y ago
Do you have a call like this services.AddDbContext<T>(....) in ConfigureServices in Startup.cs ?
Tvde1
Tvde12y ago
You have to create a 2nd context probably to connect to a 2nd db
daisy
daisy2y ago
No i can't find this
Tvde1
Tvde12y ago
I wouldn't advise mixing them into one context
daisy
daisy2y ago
Ah so you mean 2 repositorycontext?
Tvde1
Tvde12y ago
Control + Shift + F and look for AddDbContext Why is your data in two different databases?
daisy
daisy2y ago
It's default from there Okay i will try to create 2nd context, thanks
Kouhai
Kouhai2y ago
You'll need to add the other context using AddDbContext
daisy
daisy2y ago
How to this?😅 I look for Ctrl + shift + AddDbContext but i don't understand
Kouhai
Kouhai2y ago
What guide did you use for EF, thinkinggura ?
Kouhai
Kouhai2y ago
Press Ctrl + Shift + F and select Entire solution from the dropdown menu
Kouhai
Kouhai2y ago
and search AddDbContext
daisy
daisy2y ago
daisy
daisy2y ago
daisy
daisy2y ago
this one, yes?
Tvde1
Tvde12y 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
Kouhai2y ago
Adding to Tvde1's answer, make sure to change the connection string
Kouhai
Kouhai2y ago
for you new context
daisy
daisy2y 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
Kouhai2y ago
You consume that context like you'd normally do blobthumbsup
daisy
daisy2y ago
And i added new dbcontext file, but it still has same error 😅
Kouhai
Kouhai2y ago
Did you change the type when duplicating AddDbContext?
daisy
daisy2y ago
yes Kou i change it to MappiEgovContext too Wait maybe i should try to the default one. Only change this one
Kouhai
Kouhai2y ago
Yeah, make sure you have changed all instances
daisy
daisy2y ago
This is right, right? But i still get same error 😅
Tvde1
Tvde12y ago
is Cache:Enable true? Then you also have to add a DbContextPool for the new context
Kouhai
Kouhai2y ago
The type is wrong 😅
Kouhai
Kouhai2y ago
it should be MappiEgovContext
Tvde1
Tvde12y ago
ah yes that too
daisy
daisy2y ago
OMG i missed catlove
Kouhai
Kouhai2y ago
blobthumbsup
daisy
daisy2y ago
It's not working😵 Should i change this to MappiEgovContext? But if i changed it, repositoryContext error
Tvde1
Tvde12y ago
yes you should change it there what is the error you get?
daisy
daisy2y ago
this
Tvde1
Tvde12y ago
your repository base needs to be updated :) maybe add a generic type of the repository itself
daisy
daisy2y ago
Can i just duplicate this?
Tvde1
Tvde12y 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
daisy
daisy2y 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😊