Entity Framework & Database Management Flashcards

1
Q

What is Entity Framework, and how does it simplify data access in .NET applications?

A

Entity Framework (EF) is an Object-Relational Mapper (ORM) that abstracts database interactions, enabling developers to interact with a database using .NET objects instead of SQL queries. It automates tasks like generating SQL, managing connections, and handling transactions. EF allows for data queries using LINQ (Language Integrated Query), making it simpler to fetch, insert, update, and delete data.

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

How do migrations work in Entity Framework?

A

Migrations in EF enable developers to evolve the database schema over time as the application evolves. This is done by creating migration files that reflect changes in the data model. Commands like Add-Migration generate a migration based on model changes, and Update-Database applies the migration to the database.

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

Can you describe the difference between Code-First and Database-First approaches in Entity Framework?

A

Code-First: The data model is created in code using classes. The database is then generated or updated based on the code structure.
Database-First: The database schema is pre-existing, and Entity Framework generates classes and DbContext based on the database schema.
Code-First allows more control over the model, while Database-First is useful when integrating with an existing database.

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

How do you handle concurrency conflicts in Entity Framework?

A

Concurrency conflicts occur when multiple users try to update the same data simultaneously. In EF, optimistic concurrency is commonly used. EF tracks the original values when data is fetched. If another update occurs before saving, EF throws a DbUpdateConcurrencyException, allowing developers to handle the conflict (e.g., retry, reject, or merge changes).

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

What are the main differences between SQL Server and Postgres?

A

Licensing: Postgres is open-source and free, while SQL Server requires commercial licensing.
Concurrency Control: Postgres uses MVCC (Multi-Version Concurrency Control), which avoids locking, while SQL Server uses pessimistic concurrency with locking mechanisms.
Platform: Postgres runs on multiple platforms (Linux, Windows, macOS), while SQL Server is mainly for Windows, though it has limited Linux support.

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

What types of relationships can you model in Entity Framework, and how are they represented?

A

EF supports one-to-one, one-to-many, and many-to-many relationships. These relationships are represented using navigation properties and foreign keys. For example:
One-to-Many: A Category can have many Products, but a Product belongs to one Category.

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