Chapter 10: Working with Data Using Entity Framework Core Flashcards

1
Q

What are the most common places to store data?

A

Rational Database Management System (RDBMS) - SQL Server, PostgreSQL, MySQL, and SQLite
& NoSql - Azure
Cosmos DB, Redis, MongoDB, and Apache Cassandra

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are two approaches to working with EF Core

A

Database First
Code First

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to create SQLite database with cli?

A

sqlite3 Northwind.db -init Northwind4SQLite.sql

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to tell Visual Studio 2022 to copy the database file to the directory that it runs the code in so that it
can find the file, but only if the database file is newer or is missing?

A

<ItemGroup>
<None>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup
</ItemGroup>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Ef core sql server database provider

A

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.SQLite
Microsoft.EntityFrameworkCore.InMemory
Microsoft.EntityFrameworkCore.Cosmos

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ef core Mysql database provider

A

MySQL.EntityFrameworkCore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Ef core Oracle DB 11.2 database provider

A

Oracle.EntityFrameworkCore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Ef core PostgreSQL database provider

A

Npgsql.EntityFrameworkCore.PostgreSQL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to defining the Northwind database context class?

A

using Microsoft.EntityFrameworkCore;
public class Northwind : DbContext
{
protected override void OnConfiguring(
DbContextOptionsBuilder optionsBuilder)
{
string path = Path.Combine(Environment.CurrentDirectory, “Northwind.db”);
string connection = $”Filename={path}”;

    optionsBuilder.UseSqlite(connection);
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the EF Core conventions to define the model?

A
  • The name of a table is assumed to match the name of a DbSet<T> property in the DbContext
    class, for example, Products.</T>
  • The names of the columns are assumed to match the names of properties in the entity model
    class, for example, ProductId.
  • The string .NET type is assumed to be a nvarchar type in the database.
  • The int .NET type is assumed to be an int type in the database.
  • The primary key is assumed to be a property that is named Id or ID, or when the entity model
    class is named Product, then the property can be named ProductId or ProductID. If this
    property is an integer type or the Guid type, then it is also assumed to be an IDENTITY column
    (a column type that automatically assigns a value when inserting)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the EF Core annotation attributes to define the model?

A

[Required]
[StringLength(50)]
[RegularExpression(expression)]
[Column(TypeName = “money”, Name = “UnitPrice”)]

example:
[Required]
[StringLength(40)]
public string ProductName { get; set; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to using the EF Core Fluent API to define the model?

A

public class Northwind : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.Property(product => product.ProductName)
.IsRequired()
.HasMaxLength(40);</Product>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to seeding data in EF Core with the Fluent API

A

modelBuilder.Entity<Product>()
.HasData(new Product
{
ProductId = 1,
ProductName = "Chai",
UnitPrice = 8.99M
});</Product>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the lazy loading feature

A

The two properties that relate the two entities, Category.Products and Product.Category, are both
marked as virtual

In Product Model
public virtual Category Category { get; set; } = null!;

in Category Model
public virtual ICollection<Product> Products { get; set; }</Product>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to adding tables to the Northwind database context class?

A

public class Northwind : DbContext
{
// these properties map to tables in the database
public DbSet<Category>? Categories { get; set; }
public DbSet<Product>? Products { get; set; }</Product></Category>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the dotnet-ef tool?

A

The .NET CLI tool named dotnet can be extended with capabilities useful for working with EF Core

17
Q

What is the meaning of scaffolding models using an existing database?

A

scaffolding create all the models & context from existing database
actually this is the reverse engineering templates tool

18
Q

What is the scaffolding command for creating models from existing database?

A

dotnet ef dbcontext scaffold “Filename=Northwind.db” Microsoft.
EntityFrameworkCore.Sqlite –table Categories –table Products –outputdir AutoGenModels –namespace WorkingWithEFCore.AutoGen –dataannotations –context Northwind

19
Q

How to query Categories table with ef core

A

IQueryable<Category>? categories;
categories = db.Categories?.Include(c => c.Products);
if ((categories is null) || (!categories.Any()))
{
Fail("No categories found.");
return;
}</Category>

foreach (Category c in categories)
{
WriteLine($”{c.CategoryName} has {c.Products.Count} products.”);
}

20
Q

How to filtering included entities?

A

IQueryable<Category>? categories = db.Categories?
.Include(c => c.Products.Where(p => p.Stock >= stock));</Category>

21
Q

How to sorting products

A

IQueryable<Product>? products = db.Products?
.Where(product => product.Cost > price)
.OrderByDescending(product => product.Cost);</Product>

22
Q

How to getting the generated SQL

A

categories.ToQueryString()

23
Q

How to logging EF Core

A

protected override void OnConfiguring(
DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.LogTo(WriteLine)
.EnableSensitiveDataLogging();

24
Q

How to filtering logs by provider-specific values

A

optionsBuilder.LogTo(WriteLine, // Console
new[] { RelationalEventId.CommandExecuting })
.EnableSensitiveDataLogging();

25
Q

How to logging with query tags

A

IQueryable<Product>? products = db.Products?
.TagWith("Products filtered by price and sorted.")
.Where(product => product.Cost > price)
.OrderByDescending(product => product.Cost);</Product>

26
Q

How to pattern matching with Like

A

IQueryable<Product>? products = db.Products?
.Where(p => EF.Functions.Like(p.ProductName, $"%{input}%"));</Product>

27
Q

How to generating a random number in queries

A

Product? p = db.Products?.FirstOrDefault(
p => p.ProductId == (int)(EF.Functions.Random() * rowCount));

28
Q

How to defining global filters

A

modelBuilder.Entity<Product>()
.HasQueryFilter(p => !p.Discontinued)</Product>

29
Q

How many loading patterns have with EF Core

A
  • Eager loading: Load data early.
  • Lazy loading: Load data automatically just before it is needed.
  • Explicit loading: Load data manually.
30
Q

How to statically import the System.Console class for all C# files?

A

<ItemGroup>
<Using></Using>
</ItemGroup>

31
Q

Eager loading entities using the Include extension method

A

IQueryable<Category>? categories = db.Categories.Include(c => c.Products);</Category>

32
Q

Enabling lazy loading

A

<PackageReference></PackageReference>

optionsBuilder.UseLazyLoadingProxies();

33
Q

Explicit loading entities using the Load method

A

using Microsoft.EntityFrameworkCore.ChangeTracking;
CollectionEntry<Category, Product> products =
db.Entry(c).Collection(c2 => c2.Products);
if (!products.IsLoaded) products.Load();