📒
Notes
Cloud ComputingData Science/AIGame Development
  • Home
  • Big O
  • Data Structures & Algorithms
    • Data Structures
      • Array
      • Stack
      • Queue
      • Linked List
      • Binary Tree
    • Algorithms
      • Searching
      • Sorting
      • Graphs
        • Searching
        • Minimum Spanning Tree
        • Shortest Path Algorithms
      • String Algorithms
  • Object Oriented Programming
  • Languages
    • HTML/CSS
      • CSS
    • C++
    • C#
      • Types
      • Keywords
        • Modifiers
          • Access Modifiers
        • Method Parameters
      • Operators and Expressions
      • Collections
      • Constructors
      • Delegates
      • Indexers
      • Concepts
      • Features
        • LINQ
          • Operators
          • Working with Data
          • Methods
          • Resources
        • Asynchronous Programming
        • Reflection
    • Dart
    • GraphQL
    • JavaScript
      • Variable and Parameter
      • Built-in objects
        • Array
        • Built-in Functions
      • Functions
      • Classes
      • Prototype
      • Libraries
        • jQuery
        • React
          • Components
          • State and Lifecycle
          • Hooks
            • useState
            • useEffect
          • Resources
      • Testing Framework
      • Web APIs
    • Kotlin
      • Basics
    • Python
      • Basics
      • Data Structures
      • Functions
      • Resources
        • Flask
    • SQL
      • Basics
      • Operators
      • JOINs
      • Aggregations
      • Subqueries
      • Views
      • Functions
        • Window Functions
      • Stored Procedures
      • Performance Tuning
      • Extras
    • Resources
  • 🌐Web Frameworks
    • Angular
      • Templates
      • Directives
        • Attribute Directives
        • Structural Directives
    • ASP.NET
      • Fundamentals
        • Dependency Injection
        • Middleware
        • Session & State Management
      • Web apps
        • MVC
          • Controllers
            • Filters
          • Models
            • Model Binding
            • Model Validation
          • Views
            • Tag Helpers
            • View Components
          • Features
        • Client-side development
      • Web APIs
        • Controller-based APIs
        • Minimal APIs
        • OpenAPI
        • Content Negotiation
      • SignalR
      • Host and Deploy
        • IIS
      • Security
    • Django
      • The Request/Response Cycle
    • Terminologies
      • Web Server
        • Internet Information Services
    • Resources
  • 📱App Frameworks
    • Introduction
      • Resources
    • Xamarin
      • Lifecycle
      • Custom Renderers & Effects
      • Behaviors
      • Triggers
      • Gestures
      • Commands
      • Dependency Service in XF
      • Libraries
      • Showcase
    • .NET MAUI
      • Controls
      • Navigation
      • Storage Options
  • Multi-Platform Frameworks
    • .NET
      • .NET Framework
        • ADO.NET
        • WCF
      • Fundamentals
        • Logging
        • Testing
      • Advanced
        • Asynchronous Programming
        • Parallel Programming
        • Threading
        • Memory Management
          • Garbage Collection
    • Flutter
  • Object-Relational Mappers
    • Entity Framework
      • Application Models
      • Configuration
      • Setting Up
      • Advanced
  • Databases
    • Introduction
      • DBMS Architecture
      • Normalization
      • Database Transaction Models
    • Relational Databases
      • Microsoft SQL Server
        • Basics
        • Functions
        • Stored Procedures
        • Error Handling
        • Log Shipping
        • Querying and Manipulating JSON data
        • Statements
        • Topics
        • Extras
    • Non-Relational Databases
      • MongoDB
      • Redis
        • Data Structures
        • Introduction
        • Managing Database
  • Tools
    • Version Control
      • Git
        • Setup and Config
        • Basics
          • Sharing and Updating Projects
        • Resources
      • Perforce Helix
    • GitHub
    • Powershell
  • Software Development
    • Software Development Life Cycle
    • Software Design Patterns
      • GoF Design Patterns
      • Architectural Patterns
        • MVC
        • MVVM
        • N-tier Architecture
        • Onion Architecture
        • Data Transfer Objects
      • CQRS
    • Software Design Principles
      • S.O.L.I.D. Priniciple
  • System Design
    • Topics
      • Load Balancing
  • Topics
    • JWT
    • Caching
      • Static vs Dynamic Caching
    • OSI model
      • HTTP
    • Glossary
    • API
      • SOAP
      • REST
    • Microservices
    • WebHooks
    • Practice
    • Operating Systems
      • Windows
    • Architecture
  • 🔖Bookmarks
  • 🔗Resources
Powered by GitBook
On this page
  • Contexts
  • Entities
  • References
  • Collections
  • Configuring the database provider

Was this helpful?

  1. Object-Relational Mappers
  2. Entity Framework

Setting Up

Contexts

A context is a non-abstract class that inherits from DbContext and exposes a number of entity collections in the form of DbSet<T> properties.

An important function of a context is to track changes to entities so that when we are ready to save our changes, it knows what to do. Each entity tracked by the context will be in one of the following states: unchanged, modified, added, deleted, or detached.

A context can be thought of as a sandbox in which we can make changes to a collection of entities and then apply those changes with one save operation.

public class ProjectsContext : DbContext {
  public DbSet<Resource> Resources { get; private set; }  
  public DbSet<Project> Projects { get; private set; }
  public DbSet<Customer> Customers { get; private set; }
  public DbSet<Technology> Technologies { get; private set; }
}

The DbContext class offers two public constructors, allowing the passing of context options:

public class ProjectsContext : DbContext {
  public ProjectsContext(string connectionString) : base (GetOptions(connectionString))
  {
  }

  public ProjectsContext(DbContextOptions options) : base(options)
  {
  }

  private static DbContextOptions GetOptions(string connectionString) {
    var modelBuilder = new DbContextOptionsBuilder();
    return modelBuilder.UseSqlServer(connectionString).Options;
  }
}

Context options include the specific database provider to use, its connection string, and other applicable properties.

EF Core’s DbContext class has some infrastructure methods that it calls automatically at certain times:

  • OnConfiguring: Called automatically when the context needs to configure itself—setting up providers and connection strings, for example—giving developers a chance to intervene.

  • OnModelCreating: Called automatically when Entity Framework Core is assembling the data model.

  • SaveChanges: Called explicitly when we want changes to be persisted to the underlying data store. Returns the number of records affected by the saving operations.

Entities

An entity is just a class that is mapped to an Entity Framework context, and has an identity (a property that uniquely identifies instances of it).

In domain-driven design (DDD) parlance, it is said to be an aggregate root if it is meant to be directly queried.

An entity is usually persisted on its own table and may have any number of business or validation methods.

  • An entity needs to have at least a public parameterless constructor.

  • An entity always has an identifier property, which has the same name and ends with Id.

References

A reference from one entity to another defines a bidirectional relation. There are two types of reference relations:

  • Many-to-one relationship

  • One-to-one relationship

public class Project {
  //one endpoint of a many-to-one relation.
  public Customer Customer { get; set; }
  //one endpoint of a one-to-one relation.
  public ProjectDetail Detail { get; set; }
}

public class ProjectDetail {
  //the other endpoint of a one-to-one relation.
  public Project Project { get; set; }
}

public class Customer {
  //the other endpoint of a many-to-one relation.
  public ICollection<Project> Projects { get; protected set; }
}

By merely looking at one endpoint, we cannot immediately tell what its type is (one-to-one or many-to-one), we need to look at both endpoints.

Collections

Collections of entities represent one of two possible types of bidirectional relations:

  • One-to-many relationship

  • Many-to-many relationship

public class Project 
{
    public Project() 
    {    
        ProjectResources = new HashSet<ProjectResource>();  
    }
    
    public ICollection<ProjectResource> ProjectResources { get; protected set; }
}

References and collections are collectively known as navigation properties, as opposed to scalar properties.

Configuring the database provider

Entity Framework is database-agnostic, but that means that each interested party—database manufacturers or others—must release their own providers so that Entity Framework can use them. Out of the box, Microsoft makes available providers for SQL Server 2012, including Azure SQL Database, SQL Server Express, and SQL Server Express LocalDB, but also for SQLite and In Memory.

EF determines which connection to use through a new infrastructure method of DbContext, OnConfiguring, where we can explicitly configure it. Also, you can pass the configuration using the constructor that takes a DbContextOptions parameter.

Entity Framework needs to know how it should translate entities (classes, properties, and instances) back and forth into the database (specifically, tables, columns, and records). For that, it uses a mapping, for which two APIs exist.

PreviousConfigurationNextAdvanced

Last updated 2 years ago

Was this helpful?

A domain model where its entities have only properties (data) and no methods (behavior) is sometimes called an .

Anemic Domain Model
references: one-to-one, many-to-one
collections: one-to-many