Fluent API with Aaron to Database Mapping
In the previous part, we created a project named Aaron.Domain with 2 classes are "Catalog" and "Article". And this part, we shall discuss how to use Fluent API with Aaron to database mapping.First, I create a project named Aaron.Data.Mapping, and then to install a package Aaron.Core by nuget, be like Aaron.Domain, or select "Set as StartUp Project" and then run the "Package Manager Console", type command:
Install-Package Aaron.Core
In CatalogMap class:
using System.Collections.Generic; using Aaron.Core; using Aaron.Domain.Catalogs; namespace Aaron.Data.Mapping { publicclass CatalogMap : BaseEntityTypeConfiguration<Catalog, int> { public CatalogMap() : base() { this.Property(x => x.CatalogName).HasMaxLength(255); this.Property(x => x.Description).IsMaxLength(); } } }
using System; using System.Collections.Generic; using Aaron.Core; using Aaron.Domain.Articles; namespace Aaron.Data.Mapping { publicclass ArticleMap : SEOEntityTypeConfiguration<Article, Guid> { public ArticleMap() : base() { this.Property(x => x.Title).HasMaxLength(255); this.Property(x => x.Published); this.HasRequired(a => a.Catalog) .WithMany(c => c.Articles) .HasForeignKey(ac => ac.CatalogId); } } }
- BaseEntityTypeConfiguration<T,TKey> corresponds to BaseEntity<TKey>.
- SEOEntityTypeConfiguration<T,TKey> corresponds to SEOEntity<TKey>.
public ArticleMap() : base([inherited = false]) { // any code here... }