🍂 This model configuration is used to map C# objects to the database structure.
Here's what this piece of code does ✨
modelBuilder.Entity<WorkItem>(eb => { ... });
- Creates a configuration for an entity namedWorkItem
within the data model context. All the properties and relationships of this model are defined within this block.eb.Property(wi => wi.State).IsRequired();
- Specifies that theState
property is required (NOT NULL) in the database.eb.Property(wi => wi.Area).HasColumnType("varchar200");
- Specifies the column type of theArea
property asvarchar(200)
.eb.Property(wi => wi.IterationPath).HasColumnName("Iteration_Path");
- Sets the column name in the database to "Iteration_Path" for theIterationPath
property.eb.Property(wi => wi.Efford).HasColumnType("decimal(5,2)");
- Specifies the column type of theEfford
property asdecimal(5,2)
.eb.Property(wi => wi.EndDate).HasPrecision(3);
- Sets the precision of theEndDate
column to 3 decimal places.eb.Property(wi => wi.Activity).HasMaxLength(100);
- Specifies a maximum length of 100 characters for theActivity
column.eb.Property(wi => wi.RemainingWork).HasPrecision(13,3);
- Specifies a precision of 13 total digits and 3 decimal places for theRemainingWork
column.eb.HasMany(w => w.comments) ...
- Defines a "one-to-many" relationship between theWorkItem
entity and theComment
entity.eb.HasOne(w => w.Author) ...
- Defines a "one-to-many" relationship between theWorkItem
entity and theAuthor
entity.eb.HasMany(w => w.Tags) ...
- Defines a "many-to-many" relationship between theWorkItem
entity and theTag
entity through the auxiliary entityWorkItemTag
. TheUsingEntity
method is used to customize the mapping of the many-to-many relationship.
The final UsingEntity
block configures the intermediary entity WorkItemTag
, which allows customization of the mapping of the many-to-many relationship. It also sets a default value for the PublicationDate
column using HasDefaultValueSql("getutcdate()")
.
This code is an example of model configuration in Entity Framework Core and defines how entities and their relationships will be mapped to a database.
Top comments (0)