2. Console App (Code-First) Flashcards

1
Q

What’s the role of the DbContext class in EF?

A

It practically represents a database.

To connect to a database, we write a class that derives from DbContext. Each DbContext class we write gives us access to a specific database.

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

What’s a DbSet in the context of EF?

A

Practically, a table in the database.

In DbContext, we can create properties that are type DbSet. The generic type parameter T will be an entity.

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

How do you specify a connection string for a DbContext class?

A

By overriding OnConfiguring, which takes in a DbContextOptionsBuilder parameter that we can use to set the connection string to our DB.

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

What NuGet package do you need to work with a PostgreSQL database on EF Core?

A

npsql.EntityFrameworkCore.PostgreSQL

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

What NuGet package do you need to use EF Core tools?

A

Microsoft.EntityFrameworkCore.Tools

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

How do you add migrations to a project using EF Core?

A

By running dotnet ef migrations add -p .

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

How do you apply migrations to a database using EF Core?

A

By running dotnet ef database update -p .

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

How do you create a database using EF Core code-first approach?

A

By creating a new class that derives from DbContext, configuring it by overriding the OnConfiguring base method which takes in a DbContextOptionsBuilder object for dependency injection, and then creating and running migrations.

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

How do you create a table using EF Core code-first approach?

A

By creating a new class to be used as an entity, defining properties in it to be mapped to columns, and adding a new property to your DbContext child class with the type DbSet — T being the entity class type.

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

How do you access a database table in code?

A

By instantiating a context class object and calling the DbSet property that corresponds to the table you’d like to access.

eg. 
var db = MyContexT();
var tb = db.MyTable; ~~~ ~~~
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you add a new record to the database in code?

A

By using a context object to access the table and call the Add method on it.

eg. 
var db = MyContexT();
db.MyTable.Add(new MyRecord {Name = "Foo"}); ~~~ ~~~
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

After using a context object to write data to a database, what else must you do in order for those writes to actually be applied?

A

Use the context object to save changes.

eg. db.SaveChanges();

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