This article shows how to create Entity Framework Core 2.0 MVC Web application using Visual Studio 2017, ASP.NET Core, and how to create CRUD operations.
Prerequirements
To be able to run the example from the download or build it from scratch, you need to have the following tools:
- Visual Studio 2017 latest version
- .NET Core 2.0 or above
Steps to complete this article,
- Create a solution with an MVC Core 2.0 project
- Add Models(Blog, Post)
- Add DbContect
- Update StartUp
- update project file
- add migration -[Package Manager console]
- update-database -verbose
Create a new solution and name it as BlogAspNetMvcEfCoreVs2017Solution.
Add a new ASP.NET Core web application project and name it as BlogUi.
Next, select "Web Application (Model-View-Controller)" template.
Compile and run the application and we’ll see the home page.
Add Models
We will add into the project
- A folder named EF
- A Post class
- A Blog class
- And, a DataContext class like below screenshot.
Add a Post class into the Models folder.
-
-
-
-
- namespace BlogUi.Models
- {
- public class Post
- {
- public int Id { get; set; }
- public int BlogId { get; set; }
- public string Title { get; set; }
- public string Body { get; set; }
- public Blog Blog { get; set; }
- }
- }
Add a Blog class into the Models folder.
-
-
-
- using System.Collections.Generic;
-
- namespace BlogUi.Models
- {
- public class Blog
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public string Description { get; set; }
- public ICollection<Post> Posts { get; set; } = new List<Post>();
- }
- }
Add a DataContext class into the EF folder.
-
-
-
- using BlogUi.Models;
- using Microsoft.EntityFrameworkCore;
-
- namespace BlogUi.Ef
- {
- public class DataContext : DbContext
- {
- public DataContext(DbContextOptions<DataContext> options) : base(options){}
-
- public DbSet<Blog> Blog { get; set; }
- public DbSet<Post> Post { get; set; }
- }
- }
Update Startup.cs file
We need to configure our DbContext service with a database connection string like below. Add these 2 lines into ConfigureServices method.
- var connection = @"Server=(localdb)\mssqllocaldb;Database=BlogEfDB;Trusted_Connection=True;";
- services.AddDbContext<DataContext>(options => options.UseSqlServer(connection));
Here is the completeStartup.cs file.
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.EntityFrameworkCore;
- using BlogUi.Ef;
-
- namespace BlogUi
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
-
- public IConfiguration Configuration { get; }
-
-
- public void ConfigureServices(IServiceCollection services)
- {
- var connection = @"Server=(localdb)\mssqllocaldb;Database=BlogEfDB;Trusted_Connection=True;";
- services.AddDbContext<DataContext>(options => options.UseSqlServer(connection));
- services.AddMvc();
- }
-
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseBrowserLink();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- }
-
- app.UseStaticFiles();
-
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- }
- }
- }
Then we need to update BlogUi.cproj file with DotNetCliToolReference.
- <ItemGroup>
- <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet">
- <Version>2.0.0-*</Version>
- </DotNetCliToolReference>
- </ItemGroup>
Here is the updated BlogUi.cproj file.
- <Project Sdk="Microsoft.NET.Sdk.Web">
-
- <PropertyGroup>
- <TargetFramework>netcoreapp2.0</TargetFramework>
- </PropertyGroup>
-
- <ItemGroup>
- <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
- </ItemGroup>
-
- <ItemGroup>
- <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
- </ItemGroup>
-
- <ItemGroup>
- <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet">
- <Version>2.0.0-*</Version>
- </DotNetCliToolReference>
- </ItemGroup>
- </Project>
Compile the project. It is compiled without any error.
[Package Manager console] on the Default project is BlogUi selected.
add-migration initialCreate
[Package Manager console]
update-database -verbose
Microsoft SQL Management Studio shows that our BlogEfDB database is created with Blogs and Posts tables.
Add a BlogController onto Controllers folder.
BlogsController created by scaffolding -
Let's run the project and go to the
http://localhost:63139/blogs.Add a blog item.
This will complete the testing and debuging of the Web API.
Summary
In this article, we have learned how to create Entity Framework Core 2.0 MVC Web application using Visual Studio 2017 and ASP.NET Core.
Download the source code from GitHub.
Thank you for reading this article. You may be interested in reading below Training articles too.