C
C#•4mo ago
FraznoFire

Non-Nullable type "required" double up.

Hi Guys, I'm creating a model for use with Entity Framework, VSCode is yelling at me because a property doesn't have "required", but I do have it in the square brackets above. Do these two different versions of "required" overlap? or do they genuinely serve different purposes?
Problem VS Code is having
Its idea of "fixed"
18 Replies
jcotton42
jcotton42•4mo ago
They're different, the latter enforces that the prop be set by a constructor or object initializer. The former, [Required], is metadata read by stuff like EF. It predates the required keyword and is not looked at by the compiler. EF only looks at [Required]. With that said You have nullable-reference types on, and EF does look at that, so string by itself implies non-null to EF. The [Required] is redundant. @FraznoFire If you wanted SerialNo to be nullable, you'd use string? instead of string.
FraznoFire
FraznoFireOP•4mo ago
Oh so anything that doesn't have <T>? is practically the same as [Required] as far as EF is concerned?
jcotton42
jcotton42•4mo ago
Yes, T implies not nullable, T? implies nullable.
jcotton42
jcotton42•4mo ago
I reccomend you give this article a read https://learn.microsoft.com/en-us/ef/core/modeling/
Creating and Configuring a Model - EF Core
Overview of creating and configuring a Entity Framework Core model via Fluent API, Data Annotations and conventions.
FraznoFire
FraznoFireOP•4mo ago
Will do, thank you!
jcotton42
jcotton42•4mo ago
Also, I personally lean towards the fluent API for model config over the attributes. It's more flexible, and can do everything attributes can, but attributes can't necessarily do everything the fluent api can.
FraznoFire
FraznoFireOP•4mo ago
So if I wanted to use attributes like [Key] and [DatabaseGeneratedOption...], there are FluentAPI options available for that as well?
jcotton42
jcotton42•4mo ago
For keys, unless you need a compound key, it can be inferred by convention if you named the key column Id or FooId (where Foo is the table name). But, to answer your question, yes.
FraznoFire
FraznoFireOP•4mo ago
When the docs mention using FluentAPI by creating an override method to override the model builder, where do I put that? inside the model itself?
jcotton42
jcotton42•4mo ago
OnModelCreating is in the dbcontext, if that's what you mean from the article
internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region Required
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.IsRequired();
}
#endregion
}
internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region Required
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.IsRequired();
}
#endregion
}
FraznoFire
FraznoFireOP•4mo ago
ah ok, I might stick with the Data Annotations for the time being because at least for me it's a bit easier to keep track of, since it's in the same place as the model itself
jcotton42
jcotton42•4mo ago
You can do that with fluent though. Keep reading 🙂
FraznoFire
FraznoFireOP•4mo ago
I can't find anything that says you can do it outside of the dbcontext
jcotton42
jcotton42•4mo ago
"Grouping configuration"
FraznoFire
FraznoFireOP•4mo ago
That looks to be a bit out of my depth for the moment, but I will keep it as a reference for the future, thanks for your help! Actually, why does it complain about having ‘string’ without a ‘required’, but doesn’t care about other types like GUID?
Cortex
Cortex•4mo ago
wondering the same tbh. its always string
Natashi
Natashi•4mo ago
Guid is a value type
jcotton42
jcotton42•4mo ago
Guid is a value type (if you at the docs you’ll see it’s a struct, not a class) and value types cannot be null without the ? stuff. Their default value is the zero value.
Want results from more Discord servers?
Add your server