title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
13:157
| |
13:158
|
Each package has a duration in days, as well as optional start and stop dates in which the package offer is valid. MyDestination connects packages with their destinations in the many-to-one relationship that they have with the Destination entity, while DestinationId is the external key of the same relation.
|
13:159
|
While it is not obligatory to specify the external key, it is good practice to do so since this is the only way to specify some properties of the relationship. For instance, in our case, since DestinationId is an int (not nullable type), it is obligatory.
|
13:160
|
Therefore, the relationship here is one-to-many and not (0, 1)-to-many. Defining DestinationId as int? instead of int would turn the one-to-many relationship into a (0, 1)-to-many relationship. Moreover, as we will see later in this chapter, having an explicit representation of the foreign key simplifies the update operations a lot, and some queries.
|
13:161
|
In the next section, we will explain how to define the in-memory collection that represents the database tables.
|
13:162
|
Defining the mapped collections
|
13:163
|
Once we have defined all the entities that are object-oriented representations of the database rows, we need to define the in-memory collections that represent the database tables themselves. As we mentioned in the Understanding ORM basics section, all the database operations are mapped to the operations on these collections (the Querying and updating data with Entity Framework Core section of this chapter explains how). It is enough to add a DbSet<T> collection property to our DbContext for each entity, T. Usually, the name of each of these properties is obtained by pluralizing the entity name. Thus, we need to add the following two properties to our MainDbContext:
|
13:164
|
public DbSet<Package> Packages { get; set; }
|
13:165
|
public DbSet<Destination> Destinations { get; set; }
|
13:166
| |
13:167
|
Up until now, we’ve translated database stuff into properties, classes, and data annotations. However, Entity Framework needs further information to interact with a database. The next subsection explains how to provide it.
|
13:168
|
Completing the mapping configuration
|
13:169
|
The mapping configuration information that we couldn’t specify in the entity definitions must be added with configuration code based on a fluent interface. The simplest way to add this configuration information is to add it using the OnModelCreating DbContext method. Each piece of configuration information relative to an entity, T, starts with builder.Entity<T>() and continues with a call to a method that specifies that kind of constraint. Further nested calls specify further properties of the constraint. For instance, our one-to-many relationship may be configured as follows:
|
13:170
|
builder.Entity<Destination>()
|
13:171
|
.HasMany(m => m.Packages)
|
13:172
|
.WithOne(m => m.MyDestination)
|
13:173
|
.HasForeignKey(m => m.DestinationId)
|
13:174
|
.OnDelete(DeleteBehavior.Cascade);
|
13:175
| |
13:176
|
The two sides of the relationship are specified through the navigation properties that we added to our entities. HasForeignKey specifies the external key. Finally, OnDelete specifies what to do with packages when a destination is deleted. In our case, it performs a cascade delete of all the packages related to that destination.
|
13:177
|
The same configuration can be defined by starting from the other side of the relationship, that is, starting with builder.Entity<Package>(). Clearly, the developer must choose just one of the options:
|
13:178
|
builder.Entity<Package>()
|
13:179
|
.HasOne(m => m.MyDestination)
|
13:180
|
.WithMany(m => m.Packages)
|
13:181
|
.HasForeignKey(m => m.DestinationId)
|
13:182
|
.OnDelete(DeleteBehavior.Cascade);
|
13:183
| |
13:184
|
The only difference is that the previous statement’s HasMany-WithOne methods are replaced by the HasOne-WithMany methods since we started from the other side of the relationship. Here, we can also choose the precision with which each decimal property is represented in its mapped database field. As a default, decimals are represented by 18 digits and 2 decimals. You can change this setting for each property with something like:
|
13:185
|
...
|
13:186
|
.Property(m => m.Price)
|
13:187
|
.HasPrecision(10, 3);
|
13:188
| |
13:189
|
The ModelBuilder builder object allows us to specify database indexes with something such as the following:
|
13:190
|
builder.Entity<T>()
|
13:191
|
.HasIndex(m => m.PropertyName);
|
13:192
| |
13:193
|
Multi-property indexes are defined as follows:
|
13:194
|
builder.Entity<T>()
|
13:195
|
.HasIndex(`propertyName1`, `propertyName2`, ...);
|
13:196
| |
13:197
|
Starting from version 5, indexes can also be defined with attributes applied to the class. The following is the case of a single property index:
|
13:198
|
[Index(nameof(Property), IsUnique = true)]
|
13:199
|
public class MyClass
|
13:200
|
{
|
13:201
|
public int Id { get; set; }
|
13:202
|
[MaxLength(128)]
|
13:203
|
public string Property { get; set; }
|
13:204
|
}
|
13:205
| |
13:206
| |
13:207
|
The following is the case of a multi-property index:
|
13:208
|
[Index(nameof(Property1), nameof(Property2), IsUnique = false)]
|
13:209
|
public class MyComplexIndexClass
|
13:210
|
{
|
13:211
|
public int Id { get; set; }
|
13:212
|
[MaxLength(64)]
|
13:213
|
public string Property1 { get; set; }
|
13:214
|
[MaxLength(64)]
|
13:215
|
public string Property2 { get; set; }
|
13:216
|
}
|
13:217
| |
13:218
|
Configuration options that are specific to an entity can also be grouped into separate configuration classes, one for each entity:
|
13:219
|
internal class DestinationConfiguration :
|
13:220
|
IEntityTypeConfiguration<Destination>
|
13:221
|
{
|
13:222
|
public void Configure(EntityTypeBuilder<Destination> builder)
|
13:223
|
{
|
13:224
|
builder
|
13:225
|
.HasIndex(m => m.Country);
|
13:226
|
builder
|
13:227
|
.HasIndex(m => m.Name);
|
13:228
|
...
|
13:229
|
}
|
13:230
|
}
|
13:231
| |
13:232
|
Each of these classes must implement the IEntityTypeConfiguration<> interface, whose unique method is Configure. Then, the configuration class can be declared with a class-level attribute:
|
13:233
|
[EntityTypeConfiguration(typeof(DestinationConfiguration))]
|
13:234
|
public class Destination
|
13:235
|
{
|
13:236
|
...
|
13:237
| |
13:238
|
The configuration class can also be recalled from within the OnModelCreating method of the context class:
|
13:239
|
new DestinationConfiguration()
|
13:240
|
.Configure(builder.Entity<Destination>());
|
13:241
| |
13:242
|
It is also possible to add all configuration information defined in the assembly with:
|
13:243
|
builder.ApplyConfigurationsFromAssembly(typeof(MainDbContext).Assembly);
|
13:244
| |
13:245
|
If we add all the necessary configuration information, then our OnModelCreating method will look as follows:
|
13:246
|
protected override void OnModelCreating(ModelBuilder builder)
|
13:247
|
{
|
13:248
|
builder.Entity<Destination>()
|
13:249
|
.HasMany(m => m.Packages)
|
13:250
|
.WithOne(m => m.MyDestination)
|
13:251
|
.HasForeignKey(m => m.DestinationId)
|
13:252
|
.OnDelete(DeleteBehavior.Cascade);
|
13:253
|
new DestinationConfiguration()
|
13:254
|
.Configure(builder.Entity<Destination>());
|
13:255
|
new PackageConfiguration()
|
13:256
|
.Configure(builder.Entity<Package>());
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.