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
leowest3mo ago
use fluent api its a lot better, and keeps your code clean
Pobiega
Pobiega3mo ago
Please do not use DataAnnotations 😦
Timo Martinson
Timo Martinson3mo ago
ok
Pobiega
Pobiega3mo ago
leowest
leowest3mo ago
Grouping configuration is a bless
Timo Martinson
Timo Martinson3mo ago
why?
Pobiega
Pobiega3mo ago
Only supports a limited subset of configurations. Mixes model with configuration.
Timo Martinson
Timo Martinson3mo ago
ok then I configure everything in DbContext ?
MODiX
MODiX3mo ago
Pobiega
especially look at the "Grouping configuration" part
Quoted by
React with ❌ to remove this embed.
Pobiega
Pobiega3mo ago
You create public class BlogEntityTypeConfiguration : IEntityTypeConfiguration<Blog> classes
Timo Martinson
Timo Martinson3mo ago
some examples?
Pobiega
Pobiega3mo ago
read the documentation I literally just linked holy shit dude
Timo Martinson
Timo Martinson3mo ago
k SORRY.
leowest
leowest3mo 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 Martinson3mo ago
how to set "order by"? and where?
leowest
leowest3mo ago
those are done on the context youre querying
Timo Martinson
Timo Martinson3mo ago
oh. ok
leowest
leowest3mo ago
what we linked u is for configuring the model
Timo Martinson
Timo Martinson3mo ago
good thank you
leowest
leowest3mo ago
and the last link is about how to efficiently query your data
Timo Martinson
Timo Martinson3mo 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
leowest3mo ago
dont think u want to include BlogId there
Timo Martinson
Timo Martinson3mo ago
why not?
leowest
leowest3mo ago
well it doesn't make much sense to do so
Timo Martinson
Timo Martinson3mo ago
I want the slug to be unique based on the blog
leowest
leowest3mo ago
sure but can a post belong to multiple blogs?
Timo Martinson
Timo Martinson3mo ago
nope
leowest
leowest3mo ago
u see what im going after
Timo Martinson
Timo Martinson3mo ago
not really I am used to do so (in typescript and prisma)
leowest
leowest3mo ago
sure
Timo Martinson
Timo Martinson3mo ago
in Prisma I specify @@unique([slug, blogId]) ... so? for me my example makes sense
Pobiega
Pobiega3mo 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
leowest3mo ago
ah true overlooked it
Want results from more Discord servers?
Add your server
More Posts
Why is my program showing my else statement when the response is valid?Hello, I'm currently in a college course for C# and I'm learning loops right now. I'm doing an assig✅ How To Get Notification Of When User Has Scrolled Down To End - Avalonia```xml <Border> <Grid RowDefinitions="Auto, *"> <Label Classes="Title" Content="{Binding TitleLaunch browser after build/debug container in RiderTittle, any idea how? or is it impossible? Any button or something to click to open?✅ "debug executable 'location' specified in the [project] debug profile does not exist"hello, i'm an absolute beginner with c# and am trying to work on a project for school. whenever i reriot games lol apiso i have an object that has all the summoner data. i.e: name level rank etc.. but after i edit that✅ Not Quite Understanding Model Usage```cs public class UserModel { public int Id {get; set;} public string FirstName {get;set;} pu✅ **Hello, could you help me understand `&`, `&&`, `|`, `||` in C# with this code example:**```csharp int a = 4, b = 3, c = 2, d = 1; if (a / b > 1 & c - d < 1 | a % 2 == 0) label1.Text =I want to some advicesHello, im a 3rd grade computer engineering student. I want to be a .net core back-end developer. I hHow to save data on WPF?i am trying to make a wpf save data after closed, then when i reopen the wpf, i will see the data i Drag-And-Drop Environment Like NeetCode Roadmap or ScratchI'm incredibly new to WPF. I'm trying to make an app with WPF .NET Core, and want to make an "enviro