C
C#•6mo ago
TrattTratt

ASP.NET core, Access Table that is defined in another project

I have defined and created a table ("Files") in another project than the project i wanna access the data from, is that even possible? Cuz I cant do _context.Files, bec the context doesent have Files bec its defined in another context in another project.
12 Replies
TrattTratt
TrattTrattOP•6mo ago
To clarify im using the same sql db for two projects, my web app project (where I wanna get the File from the table FIles) and my azure functions project (where I have the Files in the context and where I set the File)
Pobiega
Pobiega•6mo ago
its defined in another context in another project.
Since entity configurations are also per-context, that means EF wont be able to generate appropriate sql queries, unless those configurations are also added to "this" context. You dont need a DbSet<T> in your context thou, you can just use context.Set<T> instead, but all the caveats above apply: the type must exist, it needs to be either entirely unconfigured or have the configuration loaded in both contexts
TrattTratt
TrattTrattOP•6mo ago
is it possible to access the data? what do u mean with configuration loaded in both contexts? I tried adding DbSet FIles to this context as well but it said there already was an table named Files in db
Pobiega
Pobiega•6mo ago
if its the same actual database, and the same type (or a matching type) with the same configuration yeah thats the problem you're gonna get, it will try to generate migrations for that table
TrattTratt
TrattTrattOP•6mo ago
yes its the same db
Pobiega
Pobiega•6mo ago
it should however technically work, if you somehow avoid the migration problem manually editing the snapshot file to add the other table, for example 😄 this is going to be a nightmare to maintain, fyi
TrattTratt
TrattTrattOP•6mo ago
thanks, okay, wierd that I cant find any simple solution for just accessing data from the db
Pobiega
Pobiega•6mo ago
thats not at all what you asked thou you can execute raw sql with EF Core, or you could go down a step and use SqlConnection and maybe Dapper
TrattTratt
TrattTrattOP•6mo ago
ok
Pobiega
Pobiega•6mo ago
EF is an opinionated library that expects you to follow a lot of conventions, when you break those conventions (like accessing a type that is "owned" by a different context), you get pain. its probably easier to bypass EF if this is a one-off thing
TrattTratt
TrattTrattOP•6mo ago
okay thanks
exixt
exixt•6mo ago
dapper
Want results from more Discord servers?
Add your server