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