Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. # IT:AD:Code First:FluentAPI # * [[../|(UP)]] {{indexmenu>.#2|nsort tsort}} Example: public class OurDbContext : DbContext { protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Department>().Property(dp => dp.Name).IsRequired(); modelBuilder.Entity<Manager>().HasKey(ma => ma.ManagerCode); modelBuilder.Entity<Manager>().Property(ma => ma.Name) .IsConcurrencyToken(true) .IsVariableLength() .HasMaxLength(20); } } You can break it up quite nicely as follows: public class DepartmentTypeConfiguration : EntityTypeConfiguration<Department> { public DepartmentTypeConfiguration() { Property(d => d.Name). IsRequired(). HasMaxLength(50); .... } } WHich you can then add to your `DbContext`'s `OnModelBuilding` method as follows: protected override void OnModelCreating(ModelBuilder modelBuilder) { //Add in... modelBuilder.Configurations.Add(new DepartmentTypeConfiguration()); ... base.OnModelCreating(modelBuilder); } ## Reference ## * [Ref:MSDN:Fluent API](http://bit.ly/tNQkHJ) * [Ref:CP](http://www.codeproject.com/Articles/165720/Using-the-Code-First-Model-Configuration-Classes) /home/skysigal/public_html/data/pages/it/ad/code_first/fluentapi.txt Last modified: 2023/11/04 22:22by 127.0.0.1