Convert Figma logo to code with AI

dotnet logoefcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.

13,632
3,146
13,632
2,201

Top Related Projects

Entity Framework Core provider for PostgreSQL

Entity Framework Core provider for MySQL and MariaDB built on top of MySqlConnector

13,632

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.

2,790

.NET Transactional Document DB and Event Store on PostgreSQL

2,966

Linq to database provider.

Quick Overview

Entity Framework Core (EF Core) is Microsoft's modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, PostgreSQL, and Azure Cosmos DB.

Pros

  • Cross-platform compatibility with .NET Core
  • Supports a wide range of databases
  • Powerful LINQ query capabilities
  • Code-first and database-first approaches

Cons

  • Performance overhead compared to raw SQL in some scenarios
  • Learning curve for developers new to ORM concepts
  • Some advanced features from EF6 are not available in EF Core
  • Potential for overuse of lazy loading, leading to performance issues

Code Examples

  1. Basic LINQ query:
using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Where(b => b.Rating > 3)
        .OrderBy(b => b.Url)
        .ToList();
}
  1. Adding a new entity:
using (var context = new BloggingContext())
{
    var blog = new Blog { Url = "http://example.com" };
    context.Blogs.Add(blog);
    context.SaveChanges();
}
  1. Updating an existing entity:
using (var context = new BloggingContext())
{
    var blog = context.Blogs.First();
    blog.Url = "https://updated-example.com";
    context.SaveChanges();
}

Getting Started

  1. Install the EF Core NuGet package:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  1. Create a model and DbContext:
public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True");
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}
  1. Use migrations to create the database:
dotnet ef migrations add InitialCreate
dotnet ef database update

Competitor Comparisons

Entity Framework Core provider for PostgreSQL

Pros of efcore.pg

  • Specialized PostgreSQL support with advanced features
  • Better performance for PostgreSQL-specific operations
  • Tighter integration with PostgreSQL-exclusive functionalities

Cons of efcore.pg

  • Limited to PostgreSQL, lacks cross-database compatibility
  • Smaller community and ecosystem compared to EF Core
  • May lag behind EF Core in terms of general feature updates

Code Comparison

EF Core (general approach):

using (var context = new MyDbContext())
{
    var users = context.Users.Where(u => u.Age > 18).ToList();
}

efcore.pg (PostgreSQL-specific approach):

using (var context = new MyPostgresDbContext())
{
    var users = context.Users
        .Where(u => u.Age > 18)
        .UseNpgsqlThenOrderBy(u => u.LastName)
        .ToList();
}

The efcore.pg example showcases the use of PostgreSQL-specific extensions like UseNpgsqlThenOrderBy, which can provide performance benefits for certain operations in PostgreSQL.

EF Core is a more general-purpose ORM that supports multiple database providers, while efcore.pg is specifically tailored for PostgreSQL. The choice between them depends on your project requirements, database preferences, and the need for cross-database compatibility versus PostgreSQL-specific optimizations.

Entity Framework Core provider for MySQL and MariaDB built on top of MySqlConnector

Pros of Pomelo.EntityFrameworkCore.MySql

  • Specialized MySQL support with optimized performance
  • Active community-driven development and frequent updates
  • Supports MySQL-specific features and data types

Cons of Pomelo.EntityFrameworkCore.MySql

  • Limited to MySQL databases, less versatile than EF Core
  • May have a smaller ecosystem and fewer resources compared to EF Core
  • Potential for version compatibility issues with EF Core updates

Code Comparison

EF Core (generic approach):

services.AddDbContext<MyContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

Pomelo.EntityFrameworkCore.MySql:

services.AddDbContext<MyContext>(options =>
    options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
        ServerVersion.AutoDetect(Configuration.GetConnectionString("DefaultConnection"))));

The main difference is in the database provider method (UseSqlServer vs. UseMySql) and the additional ServerVersion parameter for Pomelo, which allows automatic detection of the MySQL server version.

Pomelo.EntityFrameworkCore.MySql is an excellent choice for projects specifically targeting MySQL databases, offering optimized performance and MySQL-specific features. However, EF Core provides broader database support and a larger ecosystem, making it more suitable for projects that may need to switch between different database providers or require extensive third-party integrations.

13,632

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.

Pros of efcore

  • More active development and frequent updates
  • Larger community and contributor base
  • Better documentation and learning resources

Cons of efcore

  • Potentially more complex due to additional features
  • May have more breaking changes between versions
  • Larger codebase, which could lead to longer build times

Code Comparison

efcore:

using var context = new BloggingContext();
var blog = new Blog { Url = "http://example.com" };
context.Blogs.Add(blog);
await context.SaveChangesAsync();

efcore:

using var context = new BloggingContext();
var blog = new Blog { Url = "http://example.com" };
context.Blogs.Add(blog);
await context.SaveChangesAsync();

Summary

Both efcore and efcore are the same repository, as there is no separate repository called "efcore>". The comparison provided above is based on the assumption that you intended to compare two different versions or branches of the efcore repository. In reality, there are no significant differences to highlight between them, as they are the same project.

Entity Framework Core (EF Core) is a lightweight, extensible, open-source, and cross-platform version of the popular Entity Framework data access technology. It serves as an object-relational mapper (O/RM) for .NET, enabling developers to work with databases using .NET objects and eliminating the need for most of the data-access code typically required.

2,790

.NET Transactional Document DB and Event Store on PostgreSQL

Pros of Marten

  • Native support for document storage and querying in PostgreSQL
  • Built-in event sourcing capabilities
  • Simpler setup and configuration for document-based scenarios

Cons of Marten

  • Limited to PostgreSQL as the backend database
  • Smaller community and ecosystem compared to EF Core
  • Less comprehensive ORM features for relational data

Code Comparison

Marten:

var document = new User { Name = "John" };
session.Store(document);
await session.SaveChangesAsync();

var user = await session.Query<User>()
    .FirstOrDefaultAsync(x => x.Name == "John");

EF Core:

var user = new User { Name = "John" };
context.Users.Add(user);
await context.SaveChangesAsync();

var foundUser = await context.Users
    .FirstOrDefaultAsync(x => x.Name == "John");

Marten excels in document-based scenarios with PostgreSQL, offering built-in event sourcing. However, it's limited to PostgreSQL and has a smaller ecosystem. EF Core provides a more comprehensive ORM experience with support for multiple databases but may require more setup for document-based use cases. The code comparison shows similar syntax for basic operations, with Marten using a session and EF Core using a context for data access.

2,966

Linq to database provider.

Pros of linq2db

  • Faster performance, especially for complex queries and bulk operations
  • More flexible database support, including legacy and non-relational databases
  • Lighter weight with fewer dependencies

Cons of linq2db

  • Smaller community and ecosystem compared to EF Core
  • Less extensive documentation and learning resources
  • Fewer high-level abstractions for complex scenarios

Code Comparison

EF Core:

using (var context = new MyDbContext())
{
    var customers = context.Customers
        .Where(c => c.City == "London")
        .ToList();
}

linq2db:

using (var db = new DataConnection())
{
    var customers = db.GetTable<Customer>()
        .Where(c => c.City == "London")
        .ToList();
}

Both libraries provide LINQ-based querying, but linq2db offers more direct access to the underlying database, potentially resulting in better performance for complex queries. EF Core, on the other hand, provides a higher level of abstraction and more features out of the box, such as change tracking and lazy loading.

While EF Core is more widely adopted and has extensive Microsoft support, linq2db offers advantages in terms of performance and flexibility, especially for projects with specific database requirements or performance-critical applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Repository

build status test results

This repository is home to the following .NET Foundation projects. These projects are maintained by Microsoft and licensed under the MIT License.

EF Entity Framework Core

latest version preview version downloads

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core works with SQL Server, Azure SQL Database, SQLite, Azure Cosmos DB, MySQL, PostgreSQL, and other databases through a provider plugin API.

Installation

EF Core is available on NuGet. Install the provider package corresponding to your target database. See the list of providers in the docs for additional databases.

dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Cosmos

Use the --version option to specify a preview version to install.

Daily builds

We recommend using the daily builds to get the latest code and provide feedback on EF Core. These builds contain latest features and bug fixes; previews and official releases lag significantly behind.

Basic usage

The following code demonstrates basic usage of EF Core. For a full tutorial configuring the DbContext, defining the model, and creating the database, see getting started in the docs.

using var db = new BloggingContext();

// Inserting data into the database
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();

// Querying
var blog = db.Blogs
    .OrderBy(b => b.BlogId)
    .First();

// Updating
blog.Url = "https://devblogs.microsoft.com/dotnet";
blog.Posts.Add(
    new Post
    {
        Title = "Hello World",
        Content = "I wrote an app using EF Core!"
    });
db.SaveChanges();

// Deleting
db.Remove(blog);
db.SaveChanges();

Build from source

Most people use EF Core by installing pre-build NuGet packages, as shown above. Alternately, the code can be built and packages can be created directly on your development machine.

Contributing

We welcome community pull requests for bug fixes, enhancements, and documentation. See How to contribute for more information.

Getting support

If you have a specific question about using these projects, we encourage you to ask it on Stack Overflow. If you encounter a bug or would like to request a feature, submit an issue. For more details, see getting support.

Microsoft.Data.Sqlite

latest version preview version downloads

Microsoft.Data.Sqlite is a lightweight ADO.NET provider for SQLite. The EF Core provider for SQLite is built on top of this library. However, it can also be used independently or with other data access libraries.

Installation

The latest stable version is available on NuGet.

dotnet add package Microsoft.Data.Sqlite

Use the --version option to specify a preview version to install.

Daily builds

We recommend using the daily builds to get the latest code and provide feedback on Microsoft.Data.Sqlite. These builds contain latest features and bug fixes; previews and official releases lag significantly behind.

Basic usage

This library implements the common ADO.NET abstractions for connections, commands, data readers, and so on. For more information, see Microsoft.Data.Sqlite on Microsoft Docs.

using var connection = new SqliteConnection("Data Source=Blogs.db");
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = "SELECT Url FROM Blogs";

using var reader = command.ExecuteReader();
while (reader.Read())
{
    var url = reader.GetString(0);
}

Build from source

Most people use Microsoft.Data.Sqlite by installing pre-build NuGet packages, as shown above. Alternately, the code can be built and packages can be created directly on your development machine.

Contributing

We welcome community pull requests for bug fixes, enhancements, and documentation. See How to contribute for more information.

Getting support

If you have a specific question about using these projects, we encourage you to ask it on Stack Overflow. If you encounter a bug or would like to request a feature, submit an issue. For more details, see getting support.

See also