EF Core Practice (using MS SQL Server)

Source: Internet
Author: User
Tags dotnet

MS SQL Server is used here, and most online uses SQLite

First, a codefirst.

New Project

Here we choose ASP. NET Core WEB Application

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

Then execute the following two commands in the Package management console

Reference Entityframeworkcore

Install-package Microsoft.entityframeworkcore

Re-reference Entityframeworkcore.sqlserver

Install-package Microsoft.EntityFrameworkCore.SqlServer
Create entity

We add a Models folder to the project.

Create a new User.cs

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

Here, I'm going to keep building new DataContext.cs for convenience.

    public class Datacontext:dbcontext    {public        DataContext (dbcontextoptions<datacontext> options)            : Base (Options)        {        } public        dbset<user> Users {get; set;}    }
Create a database

Open Startup.cs Add the following code under Configureservices:

        public void Configureservices (iservicecollection services)        {
Here is the link string to fill in the database var connection = "Data source=.;i Nitial Catalog=efcore; User Id=sa; Password=sa.123 "; Services. adddbcontext<datacontext> (options = options. Usesqlite (connection)); ADD Framework Services. Services. Addmvc (); }

When added, we'll install Microsoft.EntityFrameworkCore.Tools

Install-package Microsoft.entityframeworkcore.tools–pre

Locate the project in File Explorer and locate the Project.json file

Adding code under the Tools node

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

Effects such as

Then start creating the database using the dotnet EF command

First open the cmd window and jump to the current project folder

Input

dotnet EF Migrations Add myfirstmigration

Re-enter

  dotnet EF Database Update

So the database is created.

Note If IIS-express is running, you will encounter an error
' Mvcmovie/bin/debug/netcoreapp1.0/mvcmovie.dll ' for ' The process cannot access the file
' Mvcmovie/bin/debug/netcoreapp1. 0/mvcmovie.dll'
because it is being used by another process. '
dotnet EF Command
    • dotnet(. NET Core) is a cross-platform implementation of. NET. You can learn about it here.
    • dotnet ef migrations add InitialRun the Entity Framework. NET Core CLI Migration command and create an initialization migration. The parameter "Initial" can be any value, but this is usually used as the first (initial) database migration. This action creates a *data/migrations/_initial.cs* file that contains the migration command to add (or delete) Movie the table to the database.
    • dotnet ef database updatedotnet EF Database Update updates the databases with the migration we just created.

AddUsercontroller
 Public class Usercontroller:controller    {        private  efcoredemocontext _context;          Public Usercontroller (efcoredemocontext context)        {            = context;        }         // GET:/<controller>/         Public iactionresult Index ()        {            return  View (_context. Users.tolist ());        }    }

Add index.cshtml

@model ienumerable<efcoredemo.models.user>@{Viewbag.title="User";}<tableclass="Table"> <tr> <th>Id</th> <th> user name </th> </tr>@foreach (varIteminchModel) {        <tr> <td>@Html. Displayfor (ModelItem=item. ID)</td> <td>@Html. Displayfor (ModelItem=item. UserName)</td> </tr>    }</table>

And then you can run it.

Thanks to ASP. NET Core Development-Entity Framework (EF) Core

EF Core Practice (using MS SQL Server)

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.