EF Core basic Flashcards

(29 cards)

1
Q

Describe using migrations to Change the DB

A

this is where the code is the authoritative source of truth
.NET objects can be stored in a variety of DBs
including - postgresql, sql, azure cosmos

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

What is the entity model?

A

For the entity model, entity classes describing our entity model are built and may be stored in the models folder
The entities have primary key defined by [key] attribute

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

What is the required attribute in an entity model?

A

It can be null in the model but will not be stored/saved as a null

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

What is a navigation property?

A

A property defined in a C# entity class that holds a reference to related entities
Allows developers to traverse and manage relationships (like one-to-many or many-to-many) in an object-oriented manner.

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

What is a shadow property?

A

A shadow property in EF Core is a data field that exists in the database table and the EF Core model, but not in your C# entity class.
*Not present in the .NET class code.

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

How are shadow properties managed?

A

They are managed via the ChangeTracker API and are useful for auditing (e.g., LastUpdated) or foreign keys. They are configured in OnModelCreating using the Fluent API.

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

What is a DBContext object?

A

A DB Context object derives from dbcontext
the dbcontext object represents a DB session

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

What is EF Core migration?

A

EF Core migration creates a DB based on entities you have created in C#
Create by installing and adding in the terminal

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

What are the steps for EF Core migration?

A

Migration is created in VS, then you can run commands to update the database
EF migration history is created automatically to show what tables have been updated in the DB

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

What are the EF Core steps?

A

1 build entity classes
2. create a DBContext objects for the entities
3. Add fluent API or linq queries to the DB Context object
4. delete from DB when needed

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

Where do you create the add and save methods?

A

In the DBContext type object you created for your entity

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

Describe how to query tables in EF Core

A

Write queries to tables with fluent API or Linq
these use extension methods and lambda methods

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

What is the difference between extension methods and lambda methods?

A

Extension methods allow you to add methods to existing types (like IEnumerable<T>) without modifying the original type's source code.</T>

Lambda expressions are a concise syntax for defining an anonymous function (a function without a name) inline.

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

How do you scaffold code from existing DB?

A

Scaffolding uses EF core to work with an existing DB and reverse engineers it into entities in C#

Scaffolding treats the DB, not the code as the source of truth

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

What is the scaffold DB Context command?

A

by specifying the connection string and specifying the output directories for db context and models you can use scaffold command on the command line

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

Explain more about the scaffold command.

A

the -datadAnotation flag adds attributes to models when created / scaffolded from db

17
Q

How do you keep business logic separate from scaffolded entities?

A

use partial classes or extension methods to keep business logic separate from the scaffolded entities

this helps ensure business logic doesn’t get overwritten when you re-scaffold the entities

18
Q

What are some steps to use EF Core and .NET core to streamline dev?

A

1 in program.cs add razor pages and DB Context
2 .NET secrets manager store connection string for security
3 generate razor pages with CRUD in VS
select model and context class in VS UI
creates default CRUD pages automatically (*.cshtml)
includes ui input constraints/validation that matches constraints in the model

19
Q

what is the start working directory element in the *.csproj file?

A

It tells .net core where to start

20
Q

How do you specify a DB provider in EF Core?

A

Change build options depending on which DB provider you use

21
Q

How does EF Core keep track of changes?

A

The migrations folder contains changes made to entities and db

22
Q

What are the primary keys in EF Core?

A

EF Core auto generates primary key from an int 32 in the entity
Uniquely identifies an entity

23
Q

How do you optimize performance in EF Core?

A

disable changetracking for read only queries by
adding .AsNoTracking() method to the query
.assplitquery() split querys into multiple for better perf

24
Q

What is eager loading and lazy loading?

A

eager loading -
lazy loading
cartesian explosion bad for performance

25
When do you use eager loading and lazy loading?
use lazy loading when you may not need the data until later
26
How do you make a regular sql query?
To get a sql query use .FromSQLInterpolated() method to get a raw sql result set that is otherwise unsupported / not available
27
What is FindAsync() method?
It checks the snapshot first before checking the DB
28
What is context pooling?
It uses the .AddDBContextPool() method so existing connections can be reused
29