C#C
C#2y 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.
Database.png
Pastebin
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.
Was this page helpful?