Skip to content
This repository was archived by the owner on Nov 28, 2019. It is now read-only.

Remove question mark from target audience header to help standardize the headers #184

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
83338fd
What is EF and Who should use this tutorial sections
Oct 24, 2016
a9b8032
Completed first draft of getting-started page
Oct 24, 2016
7962d4b
Changed getting-started page to the README of getting-started directory
Oct 24, 2016
0655b2a
Editted README to be concise
arynhuck Oct 26, 2016
ee96fa5
Cut SQL comment
arynhuck Oct 26, 2016
aeef2f9
Condensed sentence
arynhuck Oct 26, 2016
ebff60f
Changed README content, added TOC
arynhuck Oct 28, 2016
f5d1f89
added table of contents section, combined paragraphs, fixed wordingof…
rdahlman3 Oct 31, 2016
99bbf2e
TOC added to README
arynhuck Oct 31, 2016
916fc34
Changing how the list of prereqs looks
arynhuck Oct 31, 2016
59dc6d7
Conciseness changes to README, emptying TOC
arynhuck Nov 1, 2016
69c725f
Merge pull request #19 from LadyNaggaga/1-getting_started_readme
helmoski Nov 1, 2016
2fc7264
Resolves #16 - Adds Intro to EF Core chapter
arynhuck Oct 27, 2016
49d9d74
Merge pull request #20 from LadyNaggaga/16-what_is_ef_core
helmoski Nov 1, 2016
bb7a1de
Merge main repo into fork
helmoski Nov 3, 2016
2b8e761
23 Installing EF Core to a computer (#24)
arynhuck Nov 3, 2016
1851ab9
Resolves #22 - Supported Database Providers
arynhuck Nov 8, 2016
6747389
Resolves #3 - Querying Data Lesson
rdahlman3 Nov 9, 2016
2ddb531
Resolves #27 - fixes broken links
arynhuck Nov 22, 2016
ddfce97
Resolves #29 - Change Query Tutorial Genre
deakgeek Nov 29, 2016
0c036fd
Resolves #35
deakgeek Dec 6, 2016
616777a
Resolves #34 - Update LibraryContext.cs to use DBHelper
deakgeek Dec 7, 2016
1240ac6
Resolves #41 - missing brace (#43)
helmoski Dec 7, 2016
d3dc811
Resolves #39 - updates tutorials to support multiple files
helmoski Dec 8, 2016
88d59fd
Remove a question mark to help standardize the target audience header…
abadami Dec 8, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions content/efcore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Entity Framework Core Tutorials

[Getting Started with Entity Framework Core](getting-started/README.md)
This tutorial covers the basics of using Entity Framework Core in a C# application.
18 changes: 18 additions & 0 deletions content/efcore/getting-started/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Getting Started with Entity Framework Core

## Who Should Read This

This tutorial is aimed at people who are learning EF Core for the first time. More advanced functionality is covered in the [docs](https://docs.microsoft.com/en-us/ef/). Before beginning this tutorial, you should understand the following topics:

- C# (see [Getting Started with C#](https://www.microsoft.com/net/tutorials/csharp/getting-started))
- Relational database concepts
- LINQ (see [Introducing LINQ](https://www.microsoft.com/net/tutorials/csharp/getting-started/linq))


## Topics Covered

This tutorial will cover the following topics:

- [Intro to EF Core](intro-to-ef-core.md)
- [Installing EF Core](installing-ef-core.md)
- [Querying Data](querying.md)
56 changes: 56 additions & 0 deletions content/efcore/getting-started/installing-ef-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Installing EF Core

In this lesson, you will learn how to install EF Core on your machine. You do not need to complete these steps to continue with the rest of the tutorial as the code examples in the other lessons are executable in-browser.

## Install .NET Core

If you haven't already, you will first need to install .NET Core on your machine. Go to the [.NET Core Page](https://www.microsoft.com/net/core) of the Microsoft website, and follow the installation instructions for your platform of choice. You can ensure .NET Core is installed by running the following command:

```
dotnet --version
```

## Create New Project

Create a new project by running the following commands (you can skip these steps if you want to add EF Core to an existing project):

```
mkdir MyEfCoreProject # Create directory for project
cd MyEfCoreProject # Navigate to project directory
dotnet new # Initialize .NET Core project (generates Startup.cs and project.json)
```

## Add EF Core to Project

To add EF Core to your project, you need to list it as a dependency in your `project.json` file. You need to add the appropriate package for your database provider as well. See [Database Providers](https://docs.microsoft.com/en-us/ef/core/providers/) in the docs for a full listing of available database providers. In this example, we use Sqlite.

```{json}
"dependencies": {
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": {
"version": "1.0.0-preview2-final",
"type": "build"
}
}
```

To add EF Core functionality to the [.NET Core Command-Line Interface](https://docs.microsoft.com/en-us/dotnet/articles/core/tools/), you need to add a package to the `tools` section of `project.json`:

```
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
```

Once you have updated `package.json`, run the following command to install your project's tools and dependencies:

```
dotnet restore
```

After running this command, EF Core should be installed on your machine. You can run the following command to ensure EF Core was installed correctly:

```
dotnet ef --help
```

9 changes: 9 additions & 0 deletions content/efcore/getting-started/intro-to-ef-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Intro to EF Core

Entity Framework Core (EF Core) is a lightweight, extensible, and cross-platform version of Entity Framework. Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. This allows developers to create and manage databases without most of the data-access code that developers usually need to write. 

EF Core and the latest version of Entity Framework (EF6.x) differ in the features they support. A comparison of the features supported by each can be found on the [feature comparison page](https://docs.microsoft.com/en-us/ef/efcore-and-ef6/features) of the docs. If you want help determining which version is best for your project, check out the ["Which One Is Right for You?"](https://docs.microsoft.com/en-us/ef/efcore-and-ef6/choosing) page.

## Database Providers

At the moment, EF Core only supports relational databases, but there are plans to add support for non-relational databases in the future. Supported database providers are listed on the [Database Providers page](https://docs.microsoft.com/en-us/ef/core/providers/) in the docs.
96 changes: 96 additions & 0 deletions content/efcore/getting-started/querying.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Querying Data

In this lesson, you'll learn how to use querying to fetch one or more items from your database. EF Core uses LINQ to query data, so it is important that you understand LINQ before going through this lesson. Check out the [LINQ lesson](../../csharp/getting-started/linq.md) in the C# Interactive Tutorial if you need a refresher.

## Example database

For this lesson, we will use a small database to allow you to try querying on your own. The database has two tables, Books and Authors, and the data is as follows:

### Books
| BookId | AuthorId | Title | Genre | PublicationYear |
|--------|----------|---------------------------------|------------------|-----------------|
| 1 | 9 | Mrs. Dalloway | Literary | 1925 |
| 2 | 6 | The Mysterious Island | Science Fiction | 1874 |
| 3 | 7 | The Blazing World | Science Fiction | 1666 |
| 4 | 1 | The Scarlet Plague | Science Fiction | 1912 |
| 5 | 8 | The Secret Adversary | Mystery | 1922 |
| 6 | 6 | An Antarctic Mystery | Mystery | 1897 |
| 7 | 5 | My Bondage and My Freedom | Narrative | 1855 |
| 8 | 3 | The Count of Monte Cristo | Adventure | 1845 |
| 9 | 10 | Minnie's Sacrifice | Historical | 1869 |
| 10 | 4 | My Antonia | Historical | 1918 |
| 11 | 4 | O Pioneers! | Historical | 1913 |
| 12 | 2 | Adventures of Huckleberry Finn | Satire | 1884 |
| 13 | 2 | The Adventures of Tom Sawyer | Satire | 1876 |
| 14 | 10 | Iola Leroy | Historical | 1892 |
| 15 | 8 | Murder on the Orient Express | Mystery | 1934 |
| 16 | 1 | The Call of the Wild | Adventure | 1903 |
| 17 | 4 | Death Comes for the Archbishop | Historical | 1927 |


### Authors
| AuthorId | FirstName | LastName |
|----------|------------|-----------|
| 1 | Jack | London |
| 2 | Mark | Twain |
| 3 | Alexandre | Dumas |
| 4 | Willa | Cather |
| 5 | Frederick | Douglass |
| 6 | Jules | Vern |
| 7 | Margaret | Cavendish |
| 8 | Agatha | Christie |
| 9 | Virginia | Woolf |
| 10 | Frances | Harper |
| 11 | Stephen | Crane |

## Loading All Entities

Let's say we want to get all of the books from our database in a C# application. Normally, we would have to write a database query in a domain-specific language, such as SQL, and then we would have to manually map the results of this query to C# objects. With EF Core, this process is much easier because it takes care of the data access code for us.

```{.snippet}
using (var context = new LibraryContext())
{
var books = context.Books.ToList();
}
```
:::repl{data-name=loading-all-entities}
:::

In order to interact with the database via EF Core, we must first create an instance of our context (`LibraryContext`). Notice that we create the context with the `using` keyword. This automatically disposes the context after the using block has finished executing. Alternatively, we could manually call `LibraryContext.dispose()`, but the `using` method is more convenient and readable. It is imperative that we dispose of the context after we are finished using it because it holds an open connection to the database.

Once we have an instance of the context, we can use it to interact with the database. To access the books in the database, we reference the relevant `DbSet` within the context - `Books` in our case - and call the `ToList` method to convert the `DbSet` to a `List`. The resulting list will contain all of the books within the database.

## Filtering Entities

Loading all of the entities from a database is useful, but there are many use cases where we only want to load a subset of the entities from the database. For example, we may want to filter books by author or genre. EF Core allows us to filter entities via the `Where` extension method. Let's look at an example where we retrieve all Historical books from the database.

```{.snippet}
using (var context = new LibraryContext())
{
var books = context.Books
.Where(b => b.Genre == "Historical")
.ToList();
}
```
:::repl{data-name=filtering-entities}
:::

We use a lambda expression within the `Where` method to detect if the `Genre` property of each book is equal to "Historical". Books that meet the criteria of the lambda expression will be included in the final result, while books that do not will be excluded.

We could retrieve all of the books like in the previous example and then filter them in our application; however, this would require us to load all of the books into memory, and it does not allow us to take advantage of our database's optimized querying functionality. Allowing the database to do what it does best and perform the filtering for us results in a significant performance increase. Thus, it is important that we filter the `DbSet` with the `Where` method before calling `ToList`.

## Loading a Single Entity

Both of our examples so far have shown how to retrieve a collection of entities. Let's look at how to retrieve a single entity based on a unique identifier.

```{.snippet}
using (var context = new LibraryContext())
{
var book = context.Books
.Single(b => b.Id == 1);
}
```
:::repl{data-name=loading-single-entity}
:::

In this example, we use the `Single` extension method to find the book with an `Id` of 1. Note that we do not need to call `ToList()` because `Single` returns a single entity. It is important to only use `Single` with unique identifiers because if multiple entities meet the success criteria a `System.InvalidOperationException` will be thrown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{
using (var context = new LibraryContext())
{
var books = context.Books
.Where(b => b.Genre == "Historical")
.ToList();

foreach (Book book in books)
{
Console.WriteLine(String.Format("{0} - {1}", book.Title, book.Genre));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Filtering Entities

This code example demonstrates how to use EF Core to query a subset of entities from the database based on specified filters.

## Code Files:
- [Program.cs](Program.cs)
- [LibraryContext.cs](../shared/LibraryContext.cs)
- [Book.cs](../shared/Book.cs)
- [Author.cs](../shared/Author.cs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{
using (var context = new LibraryContext())
{
var books = context.Books.ToList();
foreach (Book book in books)
{
Console.WriteLine(book.Title);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Loading All Entities

This code example demonstrates how to use EF Core to query all entities from the database.

## Code Files:
- [Program.cs](Program.cs)
- [LibraryContext.cs](../shared/LibraryContext.cs)
- [Book.cs](../shared/Book.cs)
- [Author.cs](../shared/Author.cs)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{
using (var context = new LibraryContext())
{
var book = context.Books
.Single(b => b.BookId == 1);
Console.WriteLine(book.Title);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Loading A Single Entity

This code example demonstrates how to use EF Core to query a single entity from the database.

## Code Files:
- [Program.cs](Program.cs)
- [LibraryContext.cs](../shared/LibraryContext.cs)
- [Book.cs](../shared/Book.cs)
- [Author.cs](../shared/Author.cs)
6 changes: 6 additions & 0 deletions content/efcore/getting-started/samples/shared/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Author
{
public int AuthorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
9 changes: 9 additions & 0 deletions content/efcore/getting-started/samples/shared/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public int PublicationYear { get; set; }

public Author Author { get; set; }
}
14 changes: 14 additions & 0 deletions content/efcore/getting-started/samples/shared/LibraryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using REPLHelper;

public class LibraryContext : DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(DBHelper.GetReadonlyDbConnectionString());
}
}
3 changes: 3 additions & 0 deletions content/efcore/getting-started/toc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [Intro to EF Core](intro-to-ef-core.md)
- [Installing EF Core](installing-ef-core.md)
- [Querying Data](querying.md)