❔ EntityFramework Code cleanup
I have lots of controllers like this, is there anyway I could clean this up?
for one I don't want to be calling the firstordefaultasync each time I have an ID parameter possibly there is somew ay to implicitly convert it while carrying context for performance sake?
Also I was thinking of moving the logic to the Logic layer But I'm not sure how I'd do validation there and return it, for example. if a user has missing permissions
16 Replies
why are you not injecting the context
using a context factory instead
Yeah but why
absolutely no clue was told to do this
have been doing it since
any idea why injecting oculd be better?
Why use more code when less code do trick?
this is less code tho xd
its maybe 3-5 line difference but still
it makes no difference truly and really
You could remove
await using var context = ContextFactory.CreateDbContext();
ye true
>design time
that's not intended to be used for your actual application
what is it inteded for in that case?
it's so EF tools can create a dbcontext assuming your normal app startup isn't available
Design-time DbContext Creation - EF Core
Strategies for creating a design-time DbContext with Entity Framework Core
I see yeah that isnt ideal then
I ll switch over to injected db context in that case
Any clue what I could do regarding the if statements?
create some action filter attribute or middleware that can check those type of things for you
I see, wouldnt that be very inefficient though?
Youd have to essentially check every possible route paramater well, every possible object that can be searched via id
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.