Entity Framework Core 快速概觀Entity Framework Core Quick Overview

Entity Framework (EF) Core 是常見 Entity Framework 資料存取技術的輕量型、可擴充且跨平台版本。Entity Framework (EF) Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology.

EF Core 可以當成物件關聯式對應程式 (O/RM),讓 .NET 開發人員使用 .NET 物件來處理資料庫,而可以省略大部分以往必須自行撰寫的資料存取程式碼。EF Core can serve as an object-relational mapper (O/RM), enabling .NET developers to work with a database using .NET objects, and eliminating the need for most of the data-access code they usually need to write.

EF Core 支援許多資料庫引擎,如需詳細資料,請參閱資料庫提供者EF Core supports many database engines, see Database Providers for details.

如果您要了解如何撰寫程式碼,則建議使用我們的其中一個快速入門指南開始使用 EF Core。If you like to learn by writing code, we'd recommend one of our Getting Started guides to get you started with EF Core.

EF Core 的新功能What is new in EF Core

若您熟悉 EF Core,並且想要直接跳到最新版本的詳細資料:If you are familiar with EF Core and want to jump straight into the details of the latest releases:

取得 Entity Framework CoreGet Entity Framework Core

請針對您想要使用的資料庫提供者,安裝 NuGet 套件Install the NuGet package for the database provider you want to use. 例如,E.g. 在命令列中使用 dotnet 工具,於跨平台開發中安裝 SQL Server 提供者:to install the SQL Server provider in cross-platform development using dotnet tool in the command line:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

或者,在 Visual Studio 中,使用套件管理員主控台:Or in Visual Studio, using the Package Manager Console:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

如需可用提供者的資訊,請參閱資料庫提供者;如需詳細安裝步驟,請參閱安裝 EF CoreSee Database Providers for information on available providers and Installing EF Core for more detailed installation steps.

模型The Model

運用 EF Core,使用模型來執行資料存取。With EF Core, data access is performed using a model. 模型包含多個實體類別以及一個代表含資料庫之工作階段的衍生內容,可讓您查詢和儲存資料。A model is made up of entity classes and a derived context that represents a session with the database, allowing you to query and save data. 若要深入了解,請參閱建立模型See Creating a Model to learn more.

您可以從現有資料庫產生模型、撰寫符合您資料庫模型的程式碼,或使用 EF 移轉從您的模型建立資料庫 (它會隨著您模型一段時間的變更逐步形成)。You can generate a model from an existing database, hand code a model to match your database, or use EF Migrations to create a database from your model (and evolve it as your model changes over time).

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace Intro
{
    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=MyDatabase;Trusted_Connection=True;");
        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        public int Rating { 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; }
    }
}

查詢Querying

使用 Language Integrated Query (LINQ),從資料庫中擷取實體類別執行個體。Instances of your entity classes are retrieved from the database using Language Integrated Query (LINQ). 若要深入了解,請參閱查詢資料See Querying Data to learn more.

using (var db = new BloggingContext())
{
    var blogs = db.Blogs
        .Where(b => b.Rating > 3)
        .OrderBy(b => b.Url)
        .ToList();
}

儲存資料Saving Data

使用您實體類別的執行個體,建立、刪除和修改資料庫中的資料。Data is created, deleted, and modified in the database using instances of your entity classes. 若要深入了解,請參閱儲存資料See Saving Data to learn more.

using (var db = new BloggingContext())
{
    var blog = new Blog { Url = "http://sample.com" };
    db.Blogs.Add(blog);
    db.SaveChanges();
}