Zobaczmy na przykład.
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? IdAddress { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string BuildingNumber { get; set; }
}
class TestContext : DbContext
{
public DbSet Persons { get; set; }
public DbSet
Addresses { get; set; }
public TestContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().ToTable("Person");
modelBuilder.Entity().HasKey(x => x.Id);
modelBuilder.Entity().Property(x => x.Id).HasColumnOrder(1);
modelBuilder.Entity().Property(x => x.FirstName).HasMaxLength(50).IsRequired().HasColumnOrder(2);
modelBuilder.Entity().Property(x => x.LastName).HasMaxLength(50).IsRequired().HasColumnOrder(3);
modelBuilder.Entity
().ToTable("Address");
modelBuilder.Entity
().HasKey(x => x.Id);
modelBuilder.Entity
().Property(x => x.Id).HasColumnOrder(1);
modelBuilder.Entity
().Property(x => x.City).HasMaxLength(50).IsRequired().HasColumnOrder(2);
modelBuilder.Entity
().Property(x => x.Street).HasMaxLength(100).IsRequired().HasColumnOrder(3);
modelBuilder.Entity
().Property(x => x.BuildingNumber).HasMaxLength(10).IsRequired().HasColumnOrder(4);
modelBuilder.Entity().HasOptional(x => x.Address).WithMany().HasForeignKey(x => x.IdAddress).WillCascadeOnDelete(true);
}
}
Widać że kodu mapującego jest już 11 linijek. Każda aplikacja (poza demonstracyjnymi :) ), składa się z większej ilości tabel = więcej kodu mapującego.
Można sobie zadać pytanie ile linii będzie mieć metoda OnModelCreating kiedy dodamy kolejne tabele z ich mapowaniem.
Aby tego uniknąć można skorzystać z klas konfiguracyjnych, które przenoszą informacje o mapowaniu poza metodę OnModelCreating:
class TestContext : DbContext
{
public DbSet Persons { get; set; }
public DbSet Addresses { get; set; }
public TestContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PersonConfiguration());
modelBuilder.Configurations.Add(new AddressConfiguration());
}
}
public class PersonConfiguration : EntityTypeConfiguration
{
public PersonConfiguration()
{
ToTable("Person");
HasKey(x => x.Id);
Property(x => x.Id).HasColumnOrder(1);
Property(x => x.FirstName).HasMaxLength(50).IsRequired().HasColumnOrder(2);
Property(x => x.LastName).HasMaxLength(50).IsRequired().HasColumnOrder(3);
HasOptional(x => x.Address).WithMany().HasForeignKey(x => x.IdAddress).WillCascadeOnDelete(true);
}
}
public class AddressConfiguration : EntityTypeConfiguration
{
public AddressConfiguration()
{
ToTable("Address");
HasKey(x => x.Id);
Property(x => x.Id).HasColumnOrder(1);
Property(x => x.City).HasMaxLength(50).IsRequired().HasColumnOrder(2);
Property(x => x.Street).HasMaxLength(100).IsRequired().HasColumnOrder(3);
Property(x => x.BuildingNumber).HasMaxLength(10).IsRequired().HasColumnOrder(4);
}
}
Kod staje się bardziej czytelny, a samo dodawania następnych konfiguracji stanowczo się upraszcza.

Brak komentarzy:
Prześlij komentarz