ApartmentConfiguration
The ApartmentConfiguration class implements IEntityTypeConfiguration<Apartment> and is responsible for configuring the Apartment entity in the database.
Key Components
- Configure Method:
- Maps the
Apartmententity to theapartmentstable. - Configures the primary key and properties like
NameandDescriptionwith specific conversions. - Configures owned entities like
Address,Price, andCleaningFeewith custom conversions for currency. - Adds a
Versionproperty for pessimistic concurrency control.
- Maps the
csharp
internal sealed class ApartmentConfiguration : IEntityTypeConfiguration<Apartment>
{
public void Configure(EntityTypeBuilder<Apartment> builder)
{
builder.ToTable("apartments");
builder.HasKey(apartment => apartment.Id);
builder.Property(apartment => apartment.Name)
.HasMaxLength(200)
.HasConversion(name => name.value, value => new Name(value));
builder.Property(apartment => apartment.Description)
.HasMaxLength(2000)
.HasConversion(description => description.value, value => new Description(value));
builder.OwnsOne(apartment => apartment.Address);
builder.OwnsOne(apartment => apartment.Price, priceBuilder =>
{
priceBuilder.Property(money => money.Currency)
.HasConversion(currency => currency.Code, code => Currency.FromCode(code));
});
builder.OwnsOne(apartment => apartment.CleaningFee, priceBuilder =>
{
priceBuilder.Property(money => money.Currency)
.HasConversion(currency => currency.Code, code => Currency.FromCode(code));
});
builder.Property<uint>("Version").IsRowVersion(); //For pessimistic concurrency
}
}