ASP. NET Core Development-entity Framework (EF) Core 1.0 Database First

Source: Internet
Author: User
Tags sqlite

The ASP. NET Core Development-entity Framework Core 1.0 Database First,asp.net Core 1.0 EF core operations databases.

The Entity Framework Core 1.0 has also been released and can be applied to. NET Core 1.0 and ASP.

When EF Core RC2 is used, the code first:http://www.cnblogs.com/linezero/p/entityframeworkcore.html

Entityframeworkcore SQLite This article also introduces the database first development with the SQLite databases.

Currently supported databases for EF Core 1.0:

    • Microsoft SQL Server
    • Sqlite
    • Postgres (Npgsql)
    • SQL Server Compact Edition
    • InMemory (for testing purposes)
    • Devart have paid providers for MySQL, Oracle, and many other databases (third party development charges)

For MySQL, Oracle these should also wait for official drivers.

If you want ASP. NET Core operation MySQL can refer to this article: http://www.cnblogs.com/linezero/p/NETCoreMySQL.html

The database first development is now officially started.

New Project

Here we choose ASP. NET Core WEB Application

Here you select the Web application , and then change the authentication to not authenticate

Referencing the entity Framework (EF) Core 1.0

Referencing EF Core Sqlite

Install-package Microsoft.EntityFrameworkCore.Sqlite

Referencing the EF Core Tool

install-PackageMicrosoft.entityframeworkcore.tools–pre

Reference EF Core Sqlite Design

Install-package Microsoft.EntityFrameworkCore.Sqlite.Design

After quoting, we add "Microsoft.EntityFrameworkCore.Tools" to the Project.json Tools node: "1.0.0-preview2-final".

"Tools": {    "  Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final ",    "Bundlerminifier.core":"2.0.238",    "Microsoft.AspNetCore.Razor.Tools":"1.0.0-preview2-final",    "Microsoft.AspNetCore.Server.IISIntegration.Tools":"1.0.0-preview2-final"  },
Generating entities

Here I will not create a new database, directly using the database of the last article, to reverse the generation of entity classes.

Copy the efcoredemo.db to the bin\debug\netcoreapp1.0 folder.

First we execute commands in the Src\efcoredbfirst folder dotnet EF to make sure the commands can be executed.

Let's build the entity:

" filename=efcoredemo.db " Microsoft.EntityFrameworkCore.Sqlite

After execution, the corresponding entity classes and corresponding context are generated in the project directory.

Project use

First we'll add the following code to the Startup.cs Configureservices method:

 public  void   Configureservices (iservicecollection services) { var  connection =  filename=            Efcoredemo.db    services. Adddbcontext   <efcoredemocontext> (options =   options.            Usesqlite (connection)) ;  //  ADD framework services.   services.        Addmvc (); }

Create a new Usercontroller

Then add a User file to the views and add the corresponding view.

Add a register Action, and then add a register view

@model efcoredbfirst.users@{viewbag.title = "user add";}<formAsp-controller= "User"asp-action= "Register"Method= "POST">    <Divclass= "Form-group">        <labelasp-for= "UserName"class= "Col-md-2 Control-label">User name:</label>        <Divclass= "Col-md-10">            <inputclass= "Form-control"asp-for= "UserName" />            <spanasp-validation-for= "UserName"class= "Text-danger"></span>        </Div>        <labelasp-for= "Password"class= "Col-md-2 Control-label">Password:</label>        <Divclass= "Col-md-10">            <inputclass= "Form-control"asp-for= "Password" />            <spanasp-validation-for= "Password"class= "Text-danger"></span>        </Div>        <Divclass= "Col-md-offset-2 col-md-10">            <inputtype= "Submit"value= "Save"class= "Btn Btn-default" />        </Div>    </Div></form>

UserController.cs

     Public classUsercontroller:controller {PrivateEfcoredemocontext _context;  PublicUsercontroller (Efcoredemocontext context) {_context=context; }        //GET:/<controller>/         PublicIactionresult Index () {returnView (_context.        Users.tolist ()); }         PublicIactionresult Register () {returnView (); } [HttpPost] [Validateantiforgerytoken] Publiciactionresult Register (Users registeruser) {if(modelstate.isvalid) {_context.                Users.add (RegisterUser); _context.                SaveChanges (); returnRedirecttoaction ("Index"); }            returnView (RegisterUser); }    }
View Code

The program runs up:

Http://localhost:5000/User/Register

List showing: index.cshtml

@model ienumerable<efcoredbfirst.users>@{Viewbag.title="User";}<tableclass="Table"> <tr> <th>Id</th> <th> user name </th> <th> password </th> </t R>@foreach (varIteminchModel) {        <tr> <td>@Html. Displayfor (ModelItem=item. ID)</td> <td>@Html. Displayfor (ModelItem=item. UserName)</td> <td>@Html. Displayfor (ModelItem=item. Password)</td> </tr>    }</table>
View Code

Http://localhost:5000/User

Reference Document: Https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

If you think this article is helpful to you, please click " recommend ", thank you.

ASP. NET Core Development-entity Framework (EF) Core 1.0 Database First

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.