-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95ab2b4
commit 5db90f8
Showing
42 changed files
with
1,298 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,185 @@ | ||
.. include:: /stub-topic.txt | ||
|
||
|stub-icon| Getting Started on ASP.NET 5 | ||
======================================== | ||
Getting Started on ASP.NET 5 | ||
============================ | ||
|
||
.. include:: /stub-notice.txt | ||
In this walkthrough, you will build an ASP.NET 5 MVC application that performs basic data access using Entity Framework. | ||
|
||
.. _issue: https://github.com/aspnet/EntityFramework.Docs/issues/3 | ||
In this article: | ||
- `Create a new project`_ | ||
- `Install Entity Framework`_ | ||
- `Create your model`_ | ||
- `Register your context with dependency injection`_ | ||
- `Create your database`_ | ||
- `Create a controller`_ | ||
- `Create views`_ | ||
- `Run the application`_ | ||
|
||
`View this article's samples on GitHub <https://github.com/aspnet/EntityFramework.Docs/tree/master/docs/getting-started/aspnet5/sample>`_. | ||
|
||
.. note:: | ||
This walkthrough uses Beta 4 of ASP.NET 5 and EF7. | ||
|
||
The Beta 5 runtime of ASP.NET 5 and EF7 is available on NuGet.org but the project template in Visual Studio 2015 RC is not compatible with Beta 5. If you want to look at upgrading from Beta 4 to Beta 5, see the `ASP.NET 5 Beta5 Now Available <http://blogs.msdn.com/b/webdev/archive/2015/06/30/asp-net-5-beta5-now-available.aspx>`_ blog post. | ||
|
||
You can find nightly builds of the EF7 code base hosted on https://www.myget.org/F/aspnetvnext/api/v2/ but the code base is rapidly changing and we do not maintain up-to-date documentation for getting started. | ||
|
||
Prerequisites | ||
------------- | ||
|
||
The following items are required to complete this walkthrough: | ||
- Visual Studio 2015 RC | ||
|
||
Create a new project | ||
-------------------- | ||
|
||
- Open Visual Studio 2015 | ||
- :menuselection:`File --> New --> Project...` | ||
- From the left menu select :menuselection:`Templates --> Visual C# --> Web` | ||
- Select the **ASP.NET Web Application** project template | ||
- Ensure you are targeting .NET 4.5.1 or later | ||
- Enter **EFGetStarted.AspNet5** as the name and click **OK** | ||
|
||
When the **New ASP.NET Project** dialog appears: | ||
- Under **ASP.NET 5 Preview Templates** select **Web Site** | ||
- Ensure that **Authentication** is set to **None** | ||
- Click **OK** | ||
|
||
.. caution:: | ||
If you use **Individual User Accounts** instead of **None** for **Authentication** then an Entity Framework model will be added to your project in `Models\\IdentityModel.cs`. | ||
|
||
Using the techniques you will learn in this walkthrough, you can chose to add a second model, or extend this existing model to contain your entity classes. | ||
|
||
Install Entity Framework | ||
---------------------------------------- | ||
To use EF7 you install the package for the database provider(s) you want to target. This walkthrough uses SQL Server. For a list of available providers see :doc:`/providers/index`. | ||
|
||
- :menuselection:`Tools --> NuGet Package Manager --> Package Manager Console` | ||
- Run ``Install-Package EntityFramework.SqlServer –Pre`` | ||
|
||
.. note:: | ||
In ASP.NET 5 projects the ``Install-Package`` will complete quickly and the package installation will occur in the background. You will see **(Restoring...)** appear next to **References** in **Solution Explorer** while the install occurs. | ||
|
||
Later in this walkthrough we will also be using some Entity Framework commands to maintain the database. So we will install the commands package as well. | ||
|
||
- Run ``Install-Package EntityFramework.Commands –Pre`` | ||
- Open **project.json** | ||
- Locate the ``commands`` section and add the ``ef`` command as shown below | ||
|
||
.. literalinclude:: aspnet5/sample/src/EFGetStarted.AspNet5/project.json | ||
:language: json | ||
:linenos: | ||
:lines: 21-27 | ||
:emphasize-lines: 4 | ||
|
||
Create your model | ||
----------------- | ||
|
||
Now it's time to define a context and entity classes that make up your model. | ||
- Right-click on the project in **Solution Explorer** and select :menuselection:`Add --> New Folder` | ||
- Enter **Models** as the name of the folder | ||
- Right-click on the **Models** folder and select :menuselection:`Add --> New Item...` | ||
- From the left menu select :menuselection:`Installed --> ASP.NET 5` | ||
- Select the **Class** item template | ||
- Enter **BloggingModel.cs** as the name and click **OK** | ||
- Replace the contents of the file with the following code | ||
|
||
.. literalinclude:: aspnet5/sample/src/EFGetStarted.AspNet5/Models/BloggingModel.cs | ||
:language: c# | ||
:linenos: | ||
|
||
Register your context with dependency injection | ||
----------------------------------------------- | ||
|
||
The concept of dependency injection is central to ASP.NET 5. Services (such as our ``BloggingContext``) are registered with dependency injection during application startup. Components that require these services (such as our MVC controllers) are then provided these services via constructor parameters or properties. | ||
|
||
.. tip:: | ||
For more information on dependency injection see the `Dependency Injection <http://docs.asp.net/en/latest/fundamentals/dependency-injection.html>`_ article on the ASP.NET site. | ||
|
||
In order for our MVC controllers to make use of ``BloggingContext`` we are going to register it as a service. | ||
- Open **Startup.cs** | ||
- Locate the ``ConfigureServices`` method | ||
- Add the lines that are highlighted in the following code | ||
|
||
.. literalinclude:: aspnet5/sample/src/EFGetStarted.AspNet5/Startup.cs | ||
:language: c# | ||
:linenos: | ||
:lines: 31-38 | ||
:emphasize-lines: 4-8 | ||
|
||
|
||
Create your database | ||
-------------------- | ||
|
||
.. caution:: | ||
|
||
The migrations experience in ASP.NET 5 is still a work-in-progress. The following steps are overly complex and will be simplified by the time we reach a stable release. | ||
|
||
Now that you have a model, you can use migrations to create a database for you. | ||
- Open a command prompt (**Windows Key + R**, type **cmd**, click **OK**) | ||
- Use the ``cd`` command to navigate to the project directory | ||
- Run ``dnvm use 1.0.0-beta4`` | ||
- Run ``dnx . ef migration add MyFirstMigration`` to scaffold a migration to create the initial set of tables for your model. | ||
- Run ``dnx . ef migration apply`` to apply the new migration to the database. Because your database doesn't exist yet, it will be created for you before the migration is applied. | ||
|
||
.. tip:: | ||
If you make future changes to your model, you can use the ``dnx . ef migration add`` command to scaffold a new migration to apply the corresponding changes to the database. Once you have checked the scaffolded code (and made any required changes), you can use the ``dnx . ef migration apply`` command to apply the changes to the database. | ||
|
||
Create a controller | ||
------------------- | ||
|
||
Next, we'll add an MVC controller that will use EF to query and save data. | ||
- Right-click on the **Controllers** folder in **Solution Explorer** and select :menuselection:`Add --> New Item...` | ||
- From the left menu select :menuselection:`Installed --> ASP.NET 5` | ||
- Select the **Class** item template | ||
- Enter **BlogsController.cs** as the name and click **OK** | ||
- Replace the contents of the file with the following code | ||
|
||
.. literalinclude:: aspnet5/sample/src/EFGetStarted.AspNet5/Controllers/BlogsController.cs | ||
:language: c# | ||
:linenos: | ||
|
||
You'll notice that the controller takes a ``BlogContext`` as a constructor parameter. ASP.NET dependency injection will take care of passing an instance of ``BlogContext`` into your controller. | ||
|
||
The controller contains an ``Index`` action, which displays all blogs in the database, and a ``Create`` action, which inserts a new blogs into the database. | ||
|
||
Create views | ||
------------ | ||
|
||
Now that we have a controller it's time to add the views that will make up the user interface. | ||
|
||
We'll start with the view for our ``Index`` action, that displays all blogs. | ||
- Right-click on the **Views** folder in **Solution Explorer** and select :menuselection:`Add --> New Folder` | ||
- Enter **Blogs** as the name of the folder | ||
- Right-click on the **Blogs** folder and select :menuselection:`Add --> New Item...` | ||
- From the left menu select :menuselection:`Installed --> ASP.NET 5` | ||
- Select the **MVC View Page** item template | ||
- Enter **Index.cshtml** as the name and click **OK** | ||
- Replace the contents of the file with the following code | ||
|
||
.. literalinclude:: aspnet5/sample/src/EFGetStarted.AspNet5/Views/Blogs/Index.cshtml | ||
:linenos: | ||
|
||
We'll also add a view for the ``Create`` action, which allows the user to enter details for a new blog. | ||
- Right-click on the **Blogs** folder and select :menuselection:`Add --> New Item...` | ||
- From the left menu select :menuselection:`Installed --> ASP.NET 5` | ||
- Select the **MVC View Page** item template | ||
- Enter **Create.cshtml** as the name and click **OK** | ||
- Replace the contents of the file with the following code | ||
|
||
.. literalinclude:: aspnet5/sample/src/EFGetStarted.AspNet5/Views/Blogs/Create.cshtml | ||
:linenos: | ||
|
||
Run the application | ||
------------------- | ||
|
||
You can now run the application to see it in action. | ||
- :menuselection:`Debug --> Start Without Debugging` | ||
- The application will build and open in a web browser | ||
- Navigate to **/Blogs** | ||
- Click **Create New** | ||
- Enter a **Url** for the new blog and click **Create** | ||
|
||
.. image:: aspnet5/_static/create.png | ||
|
||
.. image:: aspnet5/_static/index.png |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions
32
docs/getting-started/aspnet5/sample/EFGetStarted.AspNet5.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 14 | ||
VisualStudioVersion = 14.0.22823.1 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{48735D53-CA9D-4B9F-B590-F6F175B5FA28}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{890A434D-BFFA-4516-A347-73F15C6E93EB}" | ||
ProjectSection(SolutionItems) = preProject | ||
global.json = global.json | ||
EndProjectSection | ||
EndProject | ||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "EFGetStarted.AspNet5", "src\EFGetStarted.AspNet5\EFGetStarted.AspNet5.xproj", "{07452B55-732D-4B25-B5BE-D28CB342770F}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{07452B55-732D-4B25-B5BE-D28CB342770F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{07452B55-732D-4B25-B5BE-D28CB342770F}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{07452B55-732D-4B25-B5BE-D28CB342770F}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{07452B55-732D-4B25-B5BE-D28CB342770F}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{07452B55-732D-4B25-B5BE-D28CB342770F} = {48735D53-CA9D-4B9F-B590-F6F175B5FA28} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"projects": [ "src", "test" ], | ||
"sdk": { | ||
"version": "1.0.0-beta4" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...tarted/aspnet5/sample/src/EFGetStarted.AspNet5/Compiler/Preprocess/RazorPreCompilation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNet.Mvc; | ||
|
||
namespace EFGetStarted.AspNet5.Compiler.Preprocess | ||
{ | ||
// Uncomment the following class to enable pre-compilation of Razor views. | ||
// Pre-compilation may reduce the time it takes to build and launch your project. | ||
// Please note, in this pre-release of Visual Studio 2015, enabling pre-compilation may cause IntelliSense and build errors in views using Tag Helpers. | ||
|
||
//public class RazorPreCompilation : RazorPreCompileModule | ||
//{ | ||
// public RazorPreCompilation(IServiceProvider provider) : base(provider) | ||
// { | ||
// GenerateSymbols = true; | ||
// } | ||
//} | ||
} |
41 changes: 41 additions & 0 deletions
41
docs/getting-started/aspnet5/sample/src/EFGetStarted.AspNet5/Controllers/BlogsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using EFGetStarted.AspNet5.Models; | ||
using Microsoft.AspNet.Mvc; | ||
using System.Linq; | ||
|
||
namespace EFGetStarted.AspNet5.Controllers | ||
{ | ||
public class BlogsController : Controller | ||
{ | ||
private BloggingContext _context; | ||
|
||
public BlogsController(BloggingContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
return View(_context.Blogs.ToList()); | ||
} | ||
|
||
public IActionResult Create() | ||
{ | ||
return View(); | ||
} | ||
|
||
[HttpPost] | ||
[ValidateAntiForgeryToken] | ||
public IActionResult Create(Blog blog) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
_context.Blogs.Add(blog); | ||
_context.SaveChanges(); | ||
return RedirectToAction("Index"); | ||
} | ||
|
||
return View(blog); | ||
} | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
docs/getting-started/aspnet5/sample/src/EFGetStarted.AspNet5/Controllers/HomeController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNet.Mvc; | ||
|
||
namespace EFGetStarted.AspNet5.Controllers | ||
{ | ||
public class HomeController : Controller | ||
{ | ||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
public IActionResult About() | ||
{ | ||
ViewBag.Message = "Your application description page."; | ||
|
||
return View(); | ||
} | ||
|
||
public IActionResult Contact() | ||
{ | ||
ViewBag.Message = "Your contact page."; | ||
|
||
return View(); | ||
} | ||
|
||
public IActionResult Error() | ||
{ | ||
return View("~/Views/Shared/Error.cshtml"); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
docs/getting-started/aspnet5/sample/src/EFGetStarted.AspNet5/EFGetStarted.AspNet5.xproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>07452b55-732d-4b25-b5be-d28cb342770f</ProjectGuid> | ||
<RootNamespace>EFGetStarted.AspNet5</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<DevelopmentServerPort>46829</DevelopmentServerPort> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
Oops, something went wrong.