ASP. NET Core development and asp. netcore Development

Source: Internet
Author: User
Tags ibm db2

ASP. NET Core development and asp. netcore Development
EF Core 1.0 Database First http://www.cnblogs.com/linezero/p/EFCoreDBFirst.html

ASP. NET Core development-Entity Framework (EF) Core, ASP. NET Core operation database.

Entity Framework (EF) Core RC2 is also released, which can be applied to. NET Core and ASP. NET Core.

EntityFrameworkCore SQLite this article introduces the SQLite database.

Currently, EF Core supports the following databases:

  • Microsoft SQL Server
  • SQLite
  • Postgres (Npgsql)
  • SQL Server Compact Edition
  • InMemory (for testing purposes)

The following will be added:

  • MySQL
  • IBM DB2

 

The introduction is complete. It is now officially started.

Create a project

Here we chooseASP. NET Core Web Application (. NET Core)

SelectWeb ApplicationsAnd then change the authenticationNo Authentication

 

Reference Entity Framework (EF) Core

NuGet official sources support references of. NET Core RC2.

Then install it in the NuGet command line. We can also use the NuGet Package Manager for installation.

Install-Package Microsoft.EntityFrameworkCore.Sqlite –Pre
Create entity

Add a Models folder in the project.

Create a User. cs

    public class User    {        public int Id { get; set; }        public string UserName { get; set; }        public string Password { get; set; }    }

For convenience, I will continue to create DataContext. cs

    public class DataContext : DbContext    {        public DataContext(DbContextOptions<DataContext> options)            : base(options)        {        }        public DbSet<User> Users { get; set; }    }
Create a database

Open Startup. cs and add the following code under ConfigureServices:

        public void ConfigureServices(IServiceCollection services)        {            var connection = "Filename=./efcoredemo.db";            services.AddDbContext<DataContext>(options => options.UseSqlite(connection));            // Add framework services.            services.AddMvc();        }

After the configuration is completed, install Microsoft. EntityFrameworkCore. Tools.

Install-Package Microsoft.EntityFrameworkCore.Tools –Pre

After installationUnder the project. json tools Node

"tools": {    "Microsoft.EntityFrameworkCore.Tools": {      "version": "1.0.0-preview1-final",      "imports": [        "portable-net45+win8+dnxcore50",        "portable-net45+win8"      ]    },

 

Start creating a databaseDotnet ef

Open the command line of the folder,

Input

Dotnet ef migrations add MyFirstMigration

  Dotnet ef database update

In this way, we have created a database. For more commands, pleaseDotnet ef-h

Project usage

Create a UserController

Add a User File in Views and add the corresponding view.

Add a Register Action and a Register view.

@ Model EFCoreDemo. models. user @ {ViewBag. title = "Add User ";} <form asp-controller = "User" asp-action = "Register" method = "post"> <div class = "form-group"> <label asp-for = "UserName "class =" col-md-2 control-label "> User Name: </label> <div class = "col-md-10"> <input class = "form-control" asp-for = "UserName"/> <span asp-validation-for = "UserName "class =" text-danger "> </span> </div> <label asp-for =" Password "class =" col-md-2 control-label "> Password: </label> <div class = "col-md-10"> <input class = "form-control" asp-for = "Password"/> <span asp-validation-for = "Password "class =" text-danger "> </span> </div> <div class =" col-md-offset-2 col-md-10 "> <input type =" submit "value =" save "class = "btn-default"/> </div> </form>

UserController. cs

    public class UserController : Controller    {        private DataContext  _context;        public UserController(DataContext context)        {            _context = context;        }        // GET: /<controller>/        public IActionResult Index()        {            return View(_context.Users.ToList());        }        public IActionResult Register()        {            return View();        }        [HttpPost]        [ValidateAntiForgeryToken]        public IActionResult Register(User registeruser)        {            if (ModelState.IsValid)            {                _context.Users.Add(registeruser);                _context.SaveChanges();                return RedirectToAction("Index");            }            return View(registeruser);        }    }
View Code

 

Run the program:

Http: // localhost: 5000/User/Register

List display

Index. cshtml

@ Model IEnumerable <EFCoreDemo. models. user >@{ ViewBag. title = "user ";} <table class = "table"> <tr> <th> Id </th> <th> User Name </th> </tr> @ foreach (var item in Model) {<tr> <td> @ Html. displayFor (modelItem => item. id) </td> <td> @ Html. displayFor (modelItem => item. userName) </td> </tr >}</table>
View Code

 

Http: // localhost: 5000/User

Reference: https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html

If you think this article is helpful to you, click" Recommendation", Thank you.

Reference page:

Http://www.yuanjiaocheng.net/entity/entity-relations.html

Http://www.yuanjiaocheng.net/entity/entity-lifecycle.html

Http://www.yuanjiaocheng.net/entity/code-first.html

Http://www.yuanjiaocheng.net/entity/mode-first.html

Http://www.yuanjiaocheng.net/entity/database-first.html

It learning materials

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.