EF attributes vs built-in C# keywords (like required). Differences and when to use?
Essentially what the title says. In this code
I'm not sure whether I need to add a [Required] tag to the Id or if [Key] makes that redundant, or if I need to make Id required with the c# keyword. I've researched this and what I've found is that the required keyword is checked at compile time while the attributes from EF are checked during runtime and they're what's being used for validation etc.
The obvious question I have is to always use required attribute where I use the keyword in c#, or vice versa?
Another example here is that my Role will always be defined. I have the [Required] attribute for validation. But do I need to add the required modifier as well?
130 Replies
Nullable type -> non-required
Non-nullable type -> required
public int Id { get; set; }
will generate a NOT NULL
in the db
public int? Id { get; set; }
will notin my case the Id is a string
so its nullable by default?
I mean it wont be nullable but I get the warning that it might not be set
But I know it will be set
It's
string?
, which is nullable.Depending on .NET version and whether you disabled NRTs
use
= null!;
to suppress that.Nowadays, by default,
string
is non-nullableWhat's the difference between that, required and [Required]
required
has you set this property
EF doesn't look at
required
.
Non-nullable with NRTs on means the same as [Required]
.
= null!;
isn't looked at by EF, it just shuts up the compiler.[Required]
is just an annotation for EF, validation, etc. Was useful before NRTsWhats NRTs let me google
nullable reference types
the
?
on classes like string
So if non nullable with NRT enabled which in my case it is, means the same as [Required[ I don't need to add that attribute?
yes
This is what dumbass chatgippity told me
The compiler forces you to provide a value when creating an object.
Does not prevent null values at runtime (e.g., database validation).
Kinda-sorta
For backwards compatibility reasons, NRTs in C# were implemented... eh, so-so
But jcotton just said that having required as a modifier is the same as [Required] in my case?
The IDE will tell you "can't have a null there!"
But it technically can be null
I said EF doesn't look at it.
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
guid
That's also a good question lmao
System.Guid
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Be the change you want to see, make a tag 😛
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Doesn't matter for now, this is just a uni project
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
I'm a frontend dev, im shit at backend dont test me
Unless your data is distributed across multiple databases and IDs need to be unique across them all
Then GUIDs can be useful
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
wont make any difference in my case at all but ill change it
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
but im still confused as to what to use, required or [Required], or both?
Neither
Use a non-nullable type
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Again, EF does not look at
required
.
erm, no
[Required] impacts the db schema.Wym neither, how would i do validation then without manually checking for each null value?
public int Foo { get; set; }
is sufficient to generate INT Foo NOT NULL
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
And that
Foo
cannot be nullBy using
string
instead of string?
, EF sets up the db schema as NON NULL
.this is my user controller
If it cannot be null don't make it nullable
There
Your validation
I just started with the project
i have no error checking yet dont diss me
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
One thing at a time lol
but i get that warning, should i just supress it with required or =null!
= null!;
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
required
would force you to set it when creating it in C#, which doesn't make sense for a key your database will generate.man youre not really helping out by just trying your best to make fun of me
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
For database models,
= null!;
For DTOs, ViewModels, regular classes, required
hm currently its a string so i generate it but i guess ill run a migration after changing ot int
alright
Whats a ViewModel?
@Aiyoh the db already exists? and you already have migrations?
An object you pass from controller to view
Im not sure if thats connected to the mvc?
It is
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
im running this as an api
i have react on the frontend
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Not MVC then
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Thats why i have no idea what that is
Wym
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
It exists but it's a local sqlite db
Tebe, one thing at a time please.
How?
This is my Controller, the one im inheriting from
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
I assumed attributes can be inherited
Which is true right since I can access the api by api/controller
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
There was some confusion, because
Controller
is a built-in class that lets you return views
But since it's your own class, it's fineAh
I called it Controller cuz its my own class didnt know what else to call it
GenericController :harold:
Eh, it's fine
Maybe
BaseController
or something
But Controller
is fineUnknown User•4d ago
Message Not Public
Sign In & Join Server To View
Alr so from now on I'll do everything non-nullable that needs to be not null with the null! thingy
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Yeah yeah
I was about to do that now
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
So wait if I do this like so :
public string Role { get; set; } = null!;
then whats the point of the [Required] attribute
Should I never use itIt existed before null ref types.
Ahh
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
So null reference types are something new in c#
?
And nothing ever gets removed from .NET/C#
Somewhat new, yes
Since C# 8, we're currently on 13.
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Damn I thought this attribute was something new, but it was something old cuz the docs I went through on microsoft docs were using this
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
i watched a course on .net and the guy turned it off, i kept it on cuz theres no way its better with it turned off
They explicitly disabled NRTs?
the more you know
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
yes because he said that it might confuse a beginner
but im not a beginner per se, only a beginner on the backend
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
i come from javascript land and i know how important it is to guard against null/undefined behavior
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
So, Javascript? :when:
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
when setting up the frontend for this project, i set up the strictest possible configs so the guy thats working with me (we need to have a partner so im teaching him frontend) doesnt end up doing :any for everything
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
ill just remove the guid gen
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
In the auth service I check whether the role passed in is valid
so it never gets created without a role
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
thjis is where its generated
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
the createdAt is redundant I know
forgot to remove it
noted
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Yeah thats the part I understood by researching, i cant do this at compile time
Good thing I asked early because I literally only have one entity currently adn thats the User
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
Yes
But this is fine I'm not gonna deploy this to production of 10000 users
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
I have no idea
Chat gippity persuaded me
Generic repos can be useful, but not in combination with EF.
So I dont need this at all
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
EF already provides a much better one for you,
DbSet<T>
.
The only reason to use a generic repo over EF is if you plan to switch away from EF, but that's... unlikely.Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
So I need a service called UserService where I'll have methods like GetUserByEmailAsync and Ill usee the DbSet thing
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
I think our uni wants us to have a certain pattern like repository even though i didnt see the use of it
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
are repos actually in the requirements?
this is too much info for me ive literally used aspnet apis for 3 days max
no
but we need to use some sort of a pattern thats common
I think its safer to have repository like this since everyone does it, but I'll keep in mind that its not the best approach here
everyone does it like this at my uni*
the course I watched used the CQRS pattern which I didnt quite get the benefit from
He used the mediatr package
No idea what the benefit was
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View
No I was watching the course for 6-7 days but worked on the project for 3 days max
So I ran the migration changed from string to int and I get datatype mismatch errors
Is it possible to force the migration to delete all the records that cant be migrated?
Or for example later if I get some issue and I need to run a migration for a change, how do I handle this without loss of data?
I literally have one record in my users db, so I can remove it safely, but what if this happens later and I have 100+ records?
Seems to have been an issue with sqlite (I had to manually change migration to delete id column then add instead of altering it)
Unknown User•4d ago
Message Not Public
Sign In & Join Server To View