Chapter 10: Working with Data Using Entity Framework Core Flashcards
What are the most common places to store data?
Rational Database Management System (RDBMS) - SQL Server, PostgreSQL, MySQL, and SQLite
& NoSql - Azure
Cosmos DB, Redis, MongoDB, and Apache Cassandra
What are two approaches to working with EF Core
Database First
Code First
How to create SQLite database with cli?
sqlite3 Northwind.db -init Northwind4SQLite.sql
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?
<ItemGroup>
<None>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup
</ItemGroup>
Ef core sql server database provider
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.SQLite
Microsoft.EntityFrameworkCore.InMemory
Microsoft.EntityFrameworkCore.Cosmos
Ef core Mysql database provider
MySQL.EntityFrameworkCore
Ef core Oracle DB 11.2 database provider
Oracle.EntityFrameworkCore
Ef core PostgreSQL database provider
Npgsql.EntityFrameworkCore.PostgreSQL
How to defining the Northwind database context class?
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); } }
What is the EF Core conventions to define the model?
- 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)
What is the EF Core annotation attributes to define the model?
[Required]
[StringLength(50)]
[RegularExpression(expression)]
[Column(TypeName = “money”, Name = “UnitPrice”)]
example:
[Required]
[StringLength(40)]
public string ProductName { get; set; }
How to using the EF Core Fluent API to define the model?
public class Northwind : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.Property(product => product.ProductName)
.IsRequired()
.HasMaxLength(40);</Product>
How to seeding data in EF Core with the Fluent API
modelBuilder.Entity<Product>()
.HasData(new Product
{
ProductId = 1,
ProductName = "Chai",
UnitPrice = 8.99M
});</Product>
What is the lazy loading feature
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 to adding tables to the Northwind database context class?
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>