Annotate Props for Entity Framework
Which annotations do exist and how do I import them?
I especially need UNIQUE contraints for the columns: [slug, id]
33 Replies
use fluent api its a lot better, and keeps your code clean
Please do not use DataAnnotations 😦
ok
https://learn.microsoft.com/en-us/ef/core/modeling/#use-fluent-api-to-configure-a-model
especially look at the "Grouping configuration" part
Grouping configuration is a bless
why?
Only supports a limited subset of configurations. Mixes model with configuration.
ok
then I configure everything in DbContext ?
Pobiega
especially look at the "Grouping configuration" part
Quoted by
<@105026391237480448> from #Annotate Props for Entity Framework (click here)
React with ❌ to remove this embed.
You create
public class BlogEntityTypeConfiguration : IEntityTypeConfiguration<Blog>
classessome examples?
read the documentation I literally just linked
holy shit dude
k
SORRY.
EF documentation is really well written and detailed
so unlike some docs its worth taking the time to go thru and digest it
and it also have really cool info like for example things people that have no knowledge of database usually do which is select only what ur going to use
https://learn.microsoft.com/en-us/ef/core/performance/efficient-querying#project-only-properties-you-need
how to set "order by"?
and where?
those are done on the context youre querying
oh. ok
what we linked u is for configuring the model
good
thank you
and the last link is about how to efficiently query your data
I did this:
dont think u want to include BlogId there
why not?
well it doesn't make much sense to do so
I want the slug to be unique based on the blog
sure but can a post belong to multiple blogs?
nope
u see what im going after
not really
I am used to do so (in typescript and prisma)
sure
in Prisma I specify
@@unique([slug, blogId])
...
so?
for me my example makes senseShould be fine. Its a composite key - each slug must be unique per blog
its not for search speed, its for the unique constraint
ah true overlooked it