0% found this document useful (0 votes)
524 views3 pages

11.2 Fluent API Cheat Sheet

The document discusses how to configure entity relationships and properties in Entity Framework Core using the Fluent API. It provides examples of configuring primary keys, columns, one-to-many relationships, many-to-many relationships, one-to-zero/one relationships, and one-to-one relationships between entities. The Fluent API allows modeling the database schema programmatically rather than using data annotations.

Uploaded by

Luis Palmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
524 views3 pages

11.2 Fluent API Cheat Sheet

The document discusses how to configure entity relationships and properties in Entity Framework Core using the Fluent API. It provides examples of configuring primary keys, columns, one-to-many relationships, many-to-many relationships, one-to-zero/one relationships, and one-to-one relationships between entities. The Fluent API allows modeling the database schema programmatically rather than using data annotations.

Uploaded by

Luis Palmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Fluent API By: Mosh Hamedani

Basics
public  class  PlutoContext  :  DbContext

{  
       protected  override  void  OnModelCreating(DbModelBuilder  
modelBuilder)

       {

               modelBuilder

                           .Entity<Course>

                           .Property(t  =>  t.Name)

                           .IsRequired();  
       }

}  

Tables
Entity<Course>.ToTable(“tbl_Course”,  “catalog”);  

Primary Keys
Entity<Book>.HasKey(t  =>  t.Isbn);


Entity<Book>.Property(t  =>  t.Isbn)

     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);


1
Fluent API By: Mosh Hamedani

Composite Primary Keys


Entity<OrderItem>.HasKey(t  =>  new  {  t.OrderId,  t.OrderId  });  

Columns
Entity<Course>.Property(t  =>  t.Name)

           .HasColumnName(“sName”)

           .HasColumnType(“varchar”)

           .HasColumnOrder(2)

           .IsRequired()

           .HasMaxLength(255);  

One-to-many Relationship
Entity<Author>

           .HasMany(a  =>  a.Courses)

           .WithRequired(c  =>  c.Author)

           .HasForeignKey(c  =>  c.AuthorId);  

Many-to-many Relationship
Entity<Course>

           .HasMany(c  =>  c.Tags)

           .WithMany(t  =>  t.Courses)

           .Map(m  =>  

             {

                 m.ToTable(“CourseTag”);  

                 m.MapLeftKey(“CourseId”);  

                 m.MapRightKey(“TagId”);

             });


2
Fluent API By: Mosh Hamedani

One-to-zero/one Relationship
Entity<Course>

           .HasOptional(c  =>  c.Caption)

           .WithRequired(c  =>  c.Course);


One-to-one Relationship
Entity<Course>

           .HasRequired(c  =>  c.Cover)

           .WithRequiredPrincipal(c  =>  c.Course);


Entity<Cover>

           .HasRequired(c  =>  c.Course)

           .WithRequiredDependent(c  =>  c.Cover);


You might also like