Not Just a Designer: Code First in Entity Framework: Listing 2.

Using the Code First Fluent API.

protected override void OnModelCreating(DbModelBuilder DbmodelBuilder)
{
  base.OnModelCreating(DbmodelBuilder);
  DbmodelBuilder.Entity<Company>().
    Property(c => c.CompanyName).
    IsRequired().
    HasMaxLength(50).
    HasColumnName("Name");
  DbmodelBuilder.Entity<Employee>().
    Property(e => e.FirstName).
    HasColumnType("varchar").
    IsRequired();
  DbmodelBuilder.Entity<Employee>().
    Property(e => e.LastName).
    HasColumnType("varchar").
    IsRequired();         
}

About the Author

Gil Fink, Microsoft MVP, is an expert in Web development and Microsoft data platforms. He works as a senior .NET consultant and architect at Sela Group. He's currently consulting for various enterprises, where he architects and develops Web- and rich Internet application-based solutions. He conducts lectures and workshops for developers and enterprises who want to specialize in infrastructure and Web development. You can read his publications at his blog.

comments powered by Disqus

Featured

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events