- Microsoft.EntityFrameworkCore.SqlServer in DataAccess project
- Microsoft.EntityFrameworkCore.Tools in DataAccess project
- Microsoft.EntityFrameworkCore.Design in all UI projects
- Create/Change Model
- Add migration -
Add-Migration <Migration_Name>
- Remove migration before updating the database -
Remove-Migration
- Apply Migration -
Update-Database
- Rollback to the specific migration -
Update-Database <Migration_Name>
- Show all migration name and status -
Get-Migration
- Delete database -
Drop-Database
[Table("<changed_table_name>")]
[Column("<changed_column_name>")]
[Required]
[Key]
[MaxLength(<maximum_length_of_a_column>)]
[NotMapped]
[ForeignKey(<Foreign_table_property_name>)]
Child Table (BookDetail)
Parent Table (Book)
Child Table (Book)
Parent Table (Publisher)
Book (Many to Many with Author)
Author (Many to Many with Book)
AuthorBookMap class
AuthorBookMap Composit Key
Book (Many to Many with Author)
Author (Many to Many with Book)
- Table Name
modelBuilder.Entity<Fluent_BookDetail>().ToTable("Fluent_BookDetails");
- Primary Key
modelBuilder.Entity<Fluent_Book>().HasKey(x => x.Book_Id);
- Composit Primary Key
modelBuilder.Entity<AuthorBookMap>().HasKey(x => new { x.Author_Id, x.Book_Id });
- Required
modelBuilder.Entity<Fluent_Book>().Property(x => x.ISBN).IsRequired();
- Property Name Change in DB
modelBuilder.Entity<Fluent_BookDetail>().Property(x => x.NumberOfChapters).HasColumnName("NoOfChapter");
- Max Length
modelBuilder.Entity<Fluent_Book>().Property(x => x.ISBN).HasMaxLength(50);
- Not Mapped
modelBuilder.Entity<Fluent_Book>().Ignore(x => x.PriceRange);
- One to One relationship
modelBuilder.Entity<Fluent_BookDetail>().HasOne(x => x.Book).WithOne(x => x.BookDetail).HasForeignKey<Fluent_BookDetail>(x => x.Book_Id);
- One to Many relationship
modelBuilder.Entity<Fluent_Book>().HasOne(x => x.Publisher).WithMany(x => x.Books).HasForeignKey(x => x.Publisher_Id);
- Many to many relationship
modelBuilder.Entity<Fluent_AuthorBookMap>().HasOne(x => x.Book).WithMany(x => x.AuthorBookMap).HasForeignKey(x => x.Book_Id);
modelBuilder.Entity<Fluent_AuthorBookMap>().HasOne(x => x.Author).WithMany(x => x.AuthorBookMap).HasForeignKey(x => x.Author_Id);
context.Database.EnsureCreated();
create the db if not exists.context.Database.GenPendingMigrations()
context.Database.Migrate()
ifGenPendingMigrations().Count() > 0
run this to run the migration 1st.
context.Books.ToList();
Get all bookscontext.Books.First();
Get 1st book in the table. Throws exception if no data foundcontext.Books.FirstOrDefault();
Get 1st book in the table. Returns null if no data foundcontext.Books.Where(x => x.Publisher_Id == 3 && x.Price > 30)
Get book with some conditions.context.Books.Find(1);
Find()
only works on primary keycontext.Books.Single();
Same asFirst()
and throws exception when query returns more than 1 rowcontext.Books.SingleOrDefault();
Same asFirstOrDefault()
and throws exception when query returns more than 1 rowcontext.Books.Where(x => x.ISBN.Contains("12"));
context.Books.Where(x => EF.Functions.Like(x.ISBN,"12"));
decimal maxPrice = context.Books.Max(x => x.Price);
int bookCount = context.Books.Count();
context.Books.OrderBy(x => x.Title).ThenByDescending(x => x.Price);
context.Books.Skip(0).Take(2);
context.Books.Add(book);
Add 1 bookcontext.Books.Remove(book);
context.SaveChanges();