O'Reilly logo

Fluent Entity Framework by Rebecca M. Riordan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Code first fluent api syntax

I said that the example I gave on the previous page isn’t valid .NET code. It isn’t, not by a long shot. None of those methods exist, and you don’t call Fluent methods on a context object; you call them on an instance of the DbModelBuilder class that’s passed to the OnModelCreating() method of your context class.

Here’s a basic example of the real syntax.

Image

public class RecipeContext : DbContext{ public DbSet<Recipe> Recipes {get; set;} protected override void OnModelCreating(DbModelBuilder modelBuilder) {  base.OnModelCreating(modelBuilder);  modelBuilder.Entity<Recipe>().Property(t => t.RecipeName).HasMaxLength(50) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required