ASP.NET MVC

Fluent API Basic

Source: Mosh Hamedani

1. Table Name

modelBuilder.Entity().Totable("tbl_Course");

2. Table Name and Schema

modelBuilder.Entity().Totable("tbl_Course","schema");

3. Primary Keys

modelBuilder.Entity().HasKey(t => t.ISBN);

4. Composite Keys

modelBuilder.Entity().HasKey(t => new {t.OrderId, t.OrderItemId});

5. Column Name

modelBuilder.Entity()
.Property(t => t.Name)
.HasColumn("sName");

6. Column Type

modelBuilder.Entity()
.Property(t => t.Name)
.HasColumnType("varchar");

7. Column Orders

modelBuilder.Entity()
.Property(t => t.Name)
.HasColumnOrder(2);

8. Database Generated

modelBuilder.Entity()
.Property(t => t.Name)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

9. Not Null

modelBuilder.Entity()
.Property(t => t.Name)
.IsRequired();

10. Length of Strings

Limit the length

modelBuilder.Entity()
.Property(t => t.Name)
.HasMaxLength(255);

Max the length

modelBuilder.Entity()
.Property(t => t.Name)
.IsMaxLength;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s