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
leowest
leowest9mo ago
use fluent api its a lot better, and keeps your code clean
Pobiega
Pobiega9mo ago
Please do not use DataAnnotations 😦
Timo Martinson
Timo MartinsonOP9mo ago
ok
Pobiega
Pobiega9mo ago
leowest
leowest9mo ago
Grouping configuration is a bless
Timo Martinson
Timo MartinsonOP9mo ago
why?
Pobiega
Pobiega9mo ago
Only supports a limited subset of configurations. Mixes model with configuration.
Timo Martinson
Timo MartinsonOP9mo ago
ok then I configure everything in DbContext ?
MODiX
MODiX9mo ago
Pobiega
especially look at the "Grouping configuration" part
Quoted by
React with ❌ to remove this embed.
Pobiega
Pobiega9mo ago
You create public class BlogEntityTypeConfiguration : IEntityTypeConfiguration<Blog> classes
Timo Martinson
Timo MartinsonOP9mo ago
some examples?
Pobiega
Pobiega9mo ago
read the documentation I literally just linked holy shit dude
Timo Martinson
Timo MartinsonOP9mo ago
k SORRY.
leowest
leowest9mo ago
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
Timo Martinson
Timo MartinsonOP9mo ago
how to set "order by"? and where?
leowest
leowest9mo ago
those are done on the context youre querying
Timo Martinson
Timo MartinsonOP9mo ago
oh. ok
leowest
leowest9mo ago
what we linked u is for configuring the model
Timo Martinson
Timo MartinsonOP9mo ago
good thank you
leowest
leowest9mo ago
and the last link is about how to efficiently query your data
Timo Martinson
Timo MartinsonOP9mo ago
I did this:
modelBuilder.Entity<Post>((entity) => {
entity.HasIndex((p) => new { p.Slug, p.BlogId }).IsUnique();
});
modelBuilder.Entity<Post>((entity) => {
entity.HasIndex((p) => new { p.Slug, p.BlogId }).IsUnique();
});
leowest
leowest9mo ago
dont think u want to include BlogId there
Timo Martinson
Timo MartinsonOP9mo ago
why not?
leowest
leowest9mo ago
well it doesn't make much sense to do so
Timo Martinson
Timo MartinsonOP9mo ago
I want the slug to be unique based on the blog
leowest
leowest9mo ago
sure but can a post belong to multiple blogs?
Timo Martinson
Timo MartinsonOP9mo ago
nope
leowest
leowest9mo ago
u see what im going after
Timo Martinson
Timo MartinsonOP9mo ago
not really I am used to do so (in typescript and prisma)
leowest
leowest9mo ago
sure
Timo Martinson
Timo MartinsonOP9mo ago
in Prisma I specify @@unique([slug, blogId]) ... so? for me my example makes sense
Pobiega
Pobiega9mo ago
Should be fine. Its a composite key - each slug must be unique per blog its not for search speed, its for the unique constraint
leowest
leowest9mo ago
ah true overlooked it
Want results from more Discord servers?
Add your server