You do not need to set the value of the primary column yourself, precisely because it is automatically generated by the database, so why do you need to do something that does not make sense? Therefore, you are making the Id
setter private. EF can still set this property when materializing an object, even if it is private.
The same story with the designer. EF requires your organization to have a parameterless design, but it can be private. But you do not want (in your example) to create an entity for the user without providing a first and last name, because most likely these names are required and you want this to be explicitly expressed. Thus, you have one constructor for creating an object (with both names) and one for EF to materialize the object obtained from the database (without parameters).
Note that both the private setter and this constructor configuration are in no way required by EF. All this is done for the convenience of developers to prevent unwanted behavior (setting the Id
field or creating an Emp
object without providing names).
source share