Entity Framework Core
repository·main·Indexed 12 days ago
https://github.com/dotnet/efcoreA modern object-database mapper for .NET. It supports LINQ queries, change tracking, and schema migrations via a DbContext. The ecosystem includes the dotnet-ef CLI tool, various database providers (SQL Server, SQLite, Azure Cosmos DB, In-Memory), and specialized packages for design-time tooling, abstractions, and proxies for lazy-loading and change-tracking.
What's inside EF Core
- Entity Framework Core (EF Core) is a modern object-database mapper for .NET (C#). It allows you to build a portable, high-level data access layer that supports LINQ queries, change tracking, updates, and schema migrations. It works across various databases including SQL Server, SQLite, MySQL, PostgreSQL, Oracle, and Azure Cosmos DB.
Use Microsoft.EntityFrameworkCore.InMemory for in-memory storage
mainThe
Microsoft.EntityFrameworkCore.InMemorypackage is a database provider that allows Entity Framework Core to operate against an in-memory database.Note: While commonly used for testing, using the in-memory provider for testing is generally discouraged. For recommended testing patterns, refer to the official Testing EF Core Applications guide.
What is ApiChief and how does it filter APIs
mainApiChief is a tool for EF Core used to manage public API baselines and review artifacts for compiled assemblies. It helps track breaking changes and generate API summaries.
When analyzing an assembly, ApiChief applies the following filtering logic:
- It only includes the public/protected API surface.
- It filters out any types located in namespaces ending with
.Internal. - It filters out any APIs decorated with the
[EntityFrameworkInternal]attribute.
Resolve connection strings via IConfiguration
mainIf you provide a connection string in the formatname=MyConnection, EF Core will use theIConfigurationservice from the service provider to resolve the actual connection string. It looks for the key"MyConnection"or"ConnectionStrings:MyConnection"within your configuration.Security considerations for Migration APIs
mainMigration APIs are designed for trusted values that are compiled into your application. They are not intended to handle untrusted input from end users.
Warning: Certain migration APIs are 'pass-thru' in nature and do not perform validation or escaping. This includes:
- The
Sql(string)method. - The
defaultValueSqlparameter.
Guidance: If you must pass input from an untrusted source to migration APIs, you must perform explicit validation to protect against SQL injection.
- The
Understand Arcade template shims and logic structure
mainArcade's template architecture is divided into three functional types to manage the difference between standard and 1ES pipelines:
- shim: An intermediate YAML file (found in
templates/ortemplates-official/) that acts as an entry point. It defines theis1ESPipelineparameter (set totruefortemplates-officialandfalsefortemplates) and redirects to the actual logic. - logic: The actual base template logic, located primarily in the
core-templates/folder. - redirect: A file in
core-templates/that redirects back to the specific logic file in eithertemplates/ortemplates-officialwhen the logic is dependent on the shim entry point used.
Key structural rule: Templates at the
stages,jobs, andjoblevels are implemented as shims. Templates at thestepsandvariableslevels typically contain direct logic because they are too granular to require shims.- shim: An intermediate YAML file (found in
Mitigate Cartesian Explosion in queries
mainRelational database JOINs can cause a "cartesian explosion," where the number of rows returned is the product of the rows in the joined tables, potentially leading to Denial of Service (DoS) through massive result sets.
To mitigate this, use the
.AsSplitQuery()method to instruct EF Core to execute multiple queries instead of a single large join.Define a DbContext and Entities
mainTo use EF Core, you must create a class that inherits from
DbContext, which represents your database session. You also define classes (entities) that represent the data structures in your database. UseDbSet<TEntity>properties within yourDbContextto expose these entities for querying and saving.using Microsoft.EntityFrameworkCore; public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; set; } } public class Customer { public int Id { get; set; } public string Name { get; set; } }Security guarantees for LINQ queries and updates
mainEF Core relational providers provide built-in protection against SQL injection through automatic parameterization and escaping:
- LINQ Queries: Any values supplied within a LINQ query (e.g., in a
.Where()clause) are automatically parameterized or escaped. - Update Pipeline: Values stored in entity properties (instance data) are automatically parameterized or escaped when using methods like
Add()andSaveChanges().
This ensures that even if values originate from untrusted end-user input, they are handled safely by the underlying database provider.
// LINQ query example: lastName is automatically parameterized public IEnumerable<Customer> FindCustomers(string lastName) { using (var context = new CustomerContext()) { return context.Customers .Where(c => c.LastName == lastName) .ToList(); } } // Update example: firstName and lastName are automatically parameterized public Customer CreateCustomer(string firstName, string lastName) { using (var context = new CustomerContext()) { var customer = new Customer { FirstName = firstName, LastName = lastName }; context.Customers.Add(customer); context.SaveChanges(); return customer; } }- LINQ Queries: Any values supplied within a LINQ query (e.g., in a
Understand EF Core security guarantees for logging and messages
mainBy default, EF Core provides the following security guarantees to prevent data leakage:
- No Credentials in Messages: Connection string credentials are never included in log messages (though database names may be).
- No Application Data in Messages: Messages (including
Exception.Message,Exception.ToString, andILogger.Writestrings) do not contain application data (e.g., query results, entity values) by default. - No Application Data in Logging State: The state passed to
ILogger.Writedoes not contain references to application data or objects from which application data can be obtained.
WARNING: These guarantees are void if the
IncludeSensitiveDataInLogflag is enabled.Understand EF Core's security guarantees regarding untrusted databases
mainEF Core is designed to handle untrusted result sets from a database. It does not load, construct, or execute code dynamically based on the data received; all types must be specified in the EF model in advance.
Key security behaviors include:
- Scaffolding Safety: The scaffolding feature (generating code from an existing database) is safe and cannot be used as a vector for Remote Code Execution (RCE) via malicious table or column names.
- Data Serialization: EF does not assume property order during deserialization. For JSON, the discriminator (
$type) is serialized first, followed by keys, in a deterministic order. - Exception Censorship: By default, EF Core censors sensitive data in exception messages, though schema information (property/column names) is not considered sensitive.
- Complexity: EF performs $O(n)$ work relative to the size of the database response ($n$).
When to use Microsoft.EntityFrameworkCore.Sqlite vs Microsoft.EntityFrameworkCore.Sqlite.Core
mainDeciding between the two SQLite provider packages depends on your requirement for the native SQLite binary:
Microsoft.EntityFrameworkCore.Sqlite: Use this for most standard applications. It automatically includes the necessary SQLite native binaries.Microsoft.EntityFrameworkCore.Sqlite.Core: Use this only if you need to swap out the default SQLite binary for a different one. This requires manual installation of a binary package and manual initialization viaSQLitePCL.Batteries_V2.Init();.