In this article, we will discuss how we can develop the user management application using ASP.Net Core with Entity Framework Core in Windows. As we discussed understanding about ASP.Net Core with hello world application on Windows in my previous articles. If you want to learn ASP.Net Core and its features, you can read the below link:

You want to download and install the free community edition of Visual Studio 2015. You can download from the Visual Studio. Furthermore, you can download .NET Core 1.0 for windows platform from .Netwebsite.

Create an ASP.NET Core Web Application on Windows

Open Visual Studio 2015. Go to File menu, point to new and click new project. New Project Window will open, you can select an installed template like “.NET Core” in Visual C# Template and then select the Asp.Net Core Web Application (.NET Core) and type Project Name ASP.NETCoreWithEFCore. Choose the project location path and click OK button.

You can select a template “Web Application” and click OK button

Now, you can see ASP.NETCoreWithEFCore project structure, as shown in the screen shot, given below

How to install the Entity Framework Core

We are going to use EF Core and install the package for the database provider from NuGet Package Manager Console in Visual Studio 2015. We will see in detail how to install EF Core for SQL Server database using the command as below steps.

If you have received error like “the term ‘Add-Migration’ is not recognized as the name of a cmdlet, then close and reopen the visual studio.

Type Update-Database and Enter

How to create our controller

Go to the Controllers folder, right click the Controllers folder and point to Add, followed by clicking the New Item. You can select an MVC Controller class and type class name as UserController and click Add button.

UserController.cs

using Microsoft.AspNetCore.Mvc;

using ASP.NETCoreWithEFCore.Models;

using System.Linq;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace ASP.NETCoreWithEFCore.Controllers

{

publicclassUserController : Controller

{

privateUserContext usercontext;

public UserController(UserContext context)

{

usercontext = context;

}

// GET: /<controller>/

publicIActionResult Index()

{

return View(usercontext.Users.ToList());

}

publicIActionResult Create()

{

return View();

}

[HttpPost]

[ValidateAntiForgeryToken]

publicIActionResult Create(User user)

{

if (ModelState.IsValid)

{

usercontext.Users.Add(user);

usercontext.SaveChanges();

return RedirectToAction("Index");

}

return View(user);

}

}

}

How to create view

We have to create a user folder in Views folder. After, you can add the view page under the views folder as below given steps.

Go to the User folder, right click the User folder and point to Add, followed by clicking the New Item. You can select an MVC View page and type file name as Index and click Add button.

Index.cshtml

@model IEnumerable<ASP.NETCoreWithEFCore.Models.User>

@{

ViewBag.Title = "User Page";

}

<h2class="panel-heading">User Dashboard</h2>

<p>

<aasp-controller="User"asp-action="Create">New User</a>

</p>

<tableclass="table table-responsive">

<tr>

<th>User Id</th>

<th>Name</th>

<th>Location</th>

</tr>

@foreach (var item in Model)

{

<tr>

<td>

@Html.DisplayFor(modelItem=> item.UserId)

</td>

<td>

@Html.DisplayFor(modelItem => item.Name)

</td>

<td>

@Html.DisplayFor(modelItem => item.Location)

</td>

</tr>

}

</table>

Go to the User folder, right click the User folder and point to Add, followed by clicking the New Item. You can select an MVC View page and type file name as Create and click Add button.

I hope you understood developing the ASP.Net Core Web application using EF Core on Windows and run on it. I have covered all the required things. If you find anything that I missed in this article, please let me know. Please share your valuable feedback or suggestions.

Share on:

About the Author
He is a Technical Lead, C# Corner MVP, Author and founder of the blog www.san2debug.net, has over 9+ years experience on Microsoft Technologies like C#.Net, ASP.Net, ADO.Net, MVC, WCF, Ajax, IIS, SQL Server, Windows Phone and some developmet tools. He is an actively spending his valuable time to popular .Net Community like C# Corner, Microsoft TechNet Wiki, DZone, Stackoverflow, DotNetSpark and etc.