Fluent API By: Mosh Hamedani
Basics
public
class
PlutoContext
:
DbContext
{
protected
override
void
OnModelCreating(DbModelBuilder
modelBuilder)
{
modelBuilder
.Entity<Course>
.Property(t
=>
[Link])
.IsRequired();
}
}
Tables
Entity<Course>.ToTable(“tbl_Course”,
“catalog”);
Primary Keys
Entity<Book>.HasKey(t
=>
[Link]);
Entity<Book>.Property(t
=>
[Link])
.HasDatabaseGeneratedOption([Link]);
1
Fluent API By: Mosh Hamedani
Composite Primary Keys
Entity<OrderItem>.HasKey(t
=>
new
{
[Link],
[Link]
});
Columns
Entity<Course>.Property(t
=>
[Link])
.HasColumnName(“sName”)
.HasColumnType(“varchar”)
.HasColumnOrder(2)
.IsRequired()
.HasMaxLength(255);
One-to-many Relationship
Entity<Author>
.HasMany(a
=>
[Link])
.WithRequired(c
=>
[Link])
.HasForeignKey(c
=>
[Link]);
Many-to-many Relationship
Entity<Course>
.HasMany(c
=>
[Link])
.WithMany(t
=>
[Link])
.Map(m
=>
{
[Link](“CourseTag”);
[Link](“CourseId”);
[Link](“TagId”);
});
2
Fluent API By: Mosh Hamedani
One-to-zero/one Relationship
Entity<Course>
.HasOptional(c
=>
[Link])
.WithRequired(c
=>
[Link]);
One-to-one Relationship
Entity<Course>
.HasRequired(c
=>
[Link])
.WithRequiredPrincipal(c
=>
[Link]);
Entity<Cover>
.HasRequired(c
=>
[Link])
.WithRequiredDependent(c
=>
[Link]);