Explore Aspnetcore+mysql+efcore

Source: Internet
Author: User

Summary

Before attempting to create a new ASP. NET core site, then how to connect with MySQL if you operate MySQL? This article will attempt to use Entityframeworkcore for MySQL operation.

An example

Start by creating an empty ASP. NET Core site,

Installing MySql.Data.EntityFrameworkCore

Http://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/7.0.5-IR21

Installation Successful

At this point you can see the MySql.Data.EntityFrameworkCore dependent configuration in the Project.json

Create a new user class

     Public class User    {        publicintsetget;}          Public string Set Get ; }          Public Set get; } = DateTime.Now;    }

Here we use DB first to do the database operation, add a test database, and create a new user table

Add the database context class.

usingMicrosoft.entityframeworkcore;usingMySQL.Data.EntityFrameworkCore.Extensions;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingWolfy.EfMySql.Models;namespacewolfy.efmysql.data{ Public classTestcontext:dbcontext { PublicDbset<user> User {Set;Get; } protected Override voidonconfiguring (Dbcontextoptionsbuilder optionsbuilder)= Optionsbuilder.usemysql (@"server=localhost;database=test;uid=root;pwd=123456"); }}

Add Controller

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMICROSOFT.ASPNETCORE.MVC;usingWolfy.EfMySql.Data;usingWolfy.EfMySql.Models;//for more information on enabling MVC for empty projects, visithttp://go.microsoft.com/fwlink/?LinkID=397860namespacewolfy.efmysql.controllers{ Public classHomecontroller:controller {//GET:/<controller>/         PublicIactionresult Index () {vardb =NewTestContext (); Db. ADD (NewUser {Name ="Hello World" }); Db.            SaveChanges (); varLST = db. Set<user>().            ToList (); returnView (LST); }    }}

Add route

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.AspNetCore.Http;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Logging;namespacewolfy.efmysql{ Public classStartup {//This method gets called by the runtime.        Use this method to add services to the container. //For more information on how to configure your application, visithttp://go.microsoft.com/fwlink/?LinkID=398940         Public voidconfigureservices (iservicecollection services) {}//This method gets called by the runtime. Use this method to configure the HTTP request pipeline.         Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Loggerf Actory.            Addconsole (); if(env. Isdevelopment ()) {app.            Usedeveloperexceptionpage (); } app. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{Controller=home}/{action=index}");        }); }    }}

Browse Index

By the above, it can be seen that the MVC service is not added, caused by, modified as follows:

         Public void configureservices (iservicecollection services)        {            services. Addmvc ();        }

View

@*    for empty projects, visit HTTP ://go.microsoft.com/fwlink/? linkid=397860*@ @model List<wolfy.efmysql.models.user><ul style="  List-style-type:none; ">    @foreach (var in Model)    {        <li> @item . Id   @item. Name   @item. Dt.tostring ("yyyy-mm-dd HH:mm:ss") </li>    }</ul>

Refresh Page

Conclusion

Here is a taste of fresh. For more information about ASP, you need to refer to the Https://www.asp.net/core

Explore Aspnetcore+mysql+efcore

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.