Chapter 10 – Working with Data Using Entity Framework Core Flashcards

1
Q

What type would you use for the property that represents a table, for example, the Products
property of a database context?

A

DbSet<T>, where T is the entity type, for example, Product.</T>

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

What type would you use for the property that represents a one-to-many relationship, for
example, the Products property of a Category entity?

A

ICollection<T>, where T is the entity type, for example, Product.</T>

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

What is the EF Core convention for primary keys?

A

The property named ID or Id or ClassNameID or ClassNameId is assumed to be the
primary key. If the type of that property is any of the following, then the property is also marked
as being an IDENTITY column: tinyint, smallint, int, bigint, and guid.

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

When might you use an annotation attribute in an entity class?

A

You might use an annotation attribute in an entity class when the conventions cannot
work out the correct mapping between the classes and tables, for example, if a class name does
not match a table name or a property name does not match a column name. You might also
define constraints, like a maximum length of characters in a text value or a range of numeric
values, by decorating with validation attributes. These can be read by technologies like ASP.NET
Core MVC and Blazor to provide automatic validation warnings to users.

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

Why might you choose Fluent API in preference to annotation attributes?

A

You might choose Fluent API in preference to annotation attributes when you want
to keep your entity classes free from extraneous code that is not needed in all scenarios. For
example, when creating a .NET Standard 2.0 class library for entity classes, you might want to
only use validation attributes so that the metadata can be read by Entity Framework Core and by
technologies like ASP.NET Core model binding validation and .NET MAUI desktop and mobile
apps. However, you might want to use Fluent API to define Entity Framework Core-specific
functionality like mapping to a different table or column name.

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

What does a transaction isolation level of Serializable mean?

A

Maximum locks are applied to ensure complete isolation from any other processes
working with the affected data.

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

What does the DbContext.SaveChanges method return?

A

An int value for the number of entities affected.

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

What is the difference between eager loading and explicit loading?

A

Eager loading means related entities are included in the original query to the database so that they do not have to be loaded later. Explicit loading means related entities are not
included in the original query to the database, and they must be explicitly loaded just before
they are needed.

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

How should you define an EF Core entity class to match the following table?
CREATE TABLE Employees(
EmpId INT IDENTITY,
FirstName NVARCHAR(40) NOT NULL,
Salary MONEY
)

A

Use the following class:
public class Employee
{
[Column(“EmpId”)]
public int EmployeeId { get; set; }
[Required]
[StringLength(40)]
public string FirstName { get; set; }
[Column(TypeName = “money”)]
public decimal? Salary { get; set; }
}

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

What benefit do you get from declaring entity navigation properties as virtual?

A

You can enable lazy loading if you declare entity navigation properties as virtual.

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