ASP. NET 5 Series tutorial (2): Hello World, asp. nethello
The content of this article is quite basic. It mainly shows you how to create an ASP. NET 5 project, including the following content:
- Create an ASP. NET 5 Project
- Add Todo Controller
- Install K Version Manager
- Perform EF migration
Open Visual Studio 2015 Preview. Select"File"Menu, selectNew>Project.
InNew ProjectIn the dialog box, clickTemplates>Visual C #>Web, SelectASP. NET Web ApplicationProject template. Name the project "TodoList" and clickOK.
Add Todo Controller
1. Download the complete project completed project.
2. In the project solution manager, right-clickControllersFolder>Add>Exiting Item.EnterTodoController. csFile Path.
3. Use the same method to addModels \ TodoItem. csAndModels \ TodoItemEditModel. csFileModelsFolder.
4. Create under ViewsToDoFolder. AddViews \ ToDoAll View files in the folderViews \ TodoFolder.
5. ChangeViews \ Shared \ _ Layout. cshtmlThe ActionLink in the file calls the Todo controller:
<!DOCTYPE html>
6. Add the DbSet to include the TodoItem modelModels \ IdentityModels. csFile.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>{private static bool _created = false;public DbSet<TodoItem> TodoItems { get; set; }// Code removed for brevity.}
7. Click to run the appTodo appLink. You will encounter the following error message (this problem will be resolved in subsequent articles ).
A database operation failed while processing the request.
SqlException: Invalid object name 'todoitem '.
There are pending model changes for ApplicationDbContext
Scaffold a new migration for these changes and apply them to the database from the command line
> K ef migration add [migration name]
> K ef migration apply
Install K Version Manager (KVM)
1. Run the Visual Studio command prompt tool as an administrator.
2. Execute the following commands:
@ Powershell-NoProfile-ExecutionPolicy unrestricted-Command "iex (new-object net. webclient). DownloadString ('https: // raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1 '))"
The preceding command installs KVM for the current user.
3.ExitVisual Studio command prompt tool. Run the Visual Studio command prompt tool as an administrator again (you need to use the new command prompt tool to obtain the updated Path Environment ).
4. Run the following command to upgrade KVM:
KVM upgrade
Now you can run EF migration ..
Perform EF migration
1. In the Administrator command prompt form, change the current operation path to the project folder, which containsProject. jsonFile.
2. Run the following command in the command prompt form:
K ef migration add initial
K ef migration apply
Ef migration add initial
The command adds a migration file named <date >_< migration name>. cs. The migration file contains the migration Code that adds The TodoItem DbSet.Migrations \ ApplicationDbContextModelSnapshot. csFile will be updated to include instructions to create theTodoItem entity.
builder.Entity("TodoList.Models.TodoItem", b => { b.Property<int>("Id") .GenerateValuesOnAdd(); b.Property<bool>("IsDone"); b.Property<int>("Priority"); b.Property<string>("Title"); b.Key("Id");});
· Run the application and clickTodo appLink. ClickCreate New TodoLink to create a newTodo.
About Todo Controller
TodoThe Controller is similar to creating an ASP. net mvc 5 project with exceptions. In this example, the data content is injectedTodoController. You can learn more from the link: Dependency Injection in ASP. NET vNext.
The above step creates a new ASP. NET 5.0 project. In the next article, we will describe how to use View Components. Coming soon.
Original article address: View components and Inject in ASP. net mvc 6