C
C#7mo ago
stevon8ter

EF Entity 'required' property

Hey there, I'm currently creating a project where I'm using EF as ORM. Take the following as an example entity: https://pastebin.com/bGZnXYvw Entity is a base class that houses our 'Id' property with a generic type - This way I can either choose int, Guid, ... The reason I use 'required string' is so that I 1. Don't get compiler warnings about needing to declare a value when exiting the constructor 2. Because the column in the Database is not-null anyway Now we're working with IEntityTypeConfiguration to set up the limitations (this way our model isn't flooded with data annotations) As we're already limiting our model properties as required, would EF recognize this? Making the IsRequired() methods on the corresponding properties irrelevant? Or should we still limit this, as this might be more expressive? I think the IsRequired() on the foreign key could be left out, as we're not using int? for this property, but correct me if I'm wrong... Are there any more improvements I could make to this? EDIT: I'm building the models from an existing database, once our old applications are finally converted we'd use code-first migrations in order to manage the database.
Pastebin
EF Required - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
No description
5 Replies
Pobiega
Pobiega7mo ago
seems easy enough to test, set one to required and one not, dont configure them (with IsRequired), generate a migration and look what it does? migrations are code, you can open them up and see what it does can then immediately throw away the migration and the changes it did after you checked
stevon8ter
stevon8terOP7mo ago
Fair point... I honestly didn't think about testing it in such a way 😅 I'll test it after lunch and post the result here (You never know someone needs the same answer)
Pobiega
Pobiega7mo ago
👍
stevon8ter
stevon8terOP7mo ago
Update: I tested this, and the migration still sets nullable: false for the properties that don't include .IsRequired() Update 2: It seems that removing the 'required' modifier from the property also doesn't affect the Nullable in migrations, this only seems to be 'true' when defining 'string?' So the required modifier in our case only makes it so that 1. Compiler warning isnt there 2. We can't create our own instance of this object without providing a value for the required properties
Davaaron
Davaaron7mo ago
How do you setup the "required" on your models? With the "keyword identifier" like public required string Name { get; set; } or like a data annotation [Required]? I'd try the latter one, because the required keyword tells the compiler that the properties need to be set in order to initialize the value, a readonly non-nullable value that needs be set before the object leaves the constructor. However, having ".IsRequired" in the configuration isn't a bad thing though, this way you have all the configurations in one place. I hate to jump around between configurations and models in suchs projects. More convenient to have everything configured the same way. Good luck!
Want results from more Discord servers?
Add your server