ASP. NET core Webapi uses EF to make a search for MySQL and generate a Docker image build container to run

Source: Internet
Author: User

1. Build the MySQL container and add the database user

Refer to Docker creation to run multiple MySQL containers, address http://www.cnblogs.com/heyangyi/p/9288402.html

Add user database, add tbusers table

2. Create an ASP. NET Core WEBAPI Application

Refer to Docker to generate Docker images for ASP. WEBAPI applications, create containers and run, address http://www.cnblogs.com/heyangyi/p/9323407.html

<2.1> Modify Appsettings.json file to add dbconn database link configuration

<2.2> new Config class for storing configuration

    public class Config    {public        static string dbconn;    }

<2.3> Modify Program class, read configuration

    public class program    {        private static iconfigurationroot Configuration {get; set;}        public static void Main (string[] args)        {            var builder = new Configurationbuilder ()             . Setbasepath (Directory.GetCurrentDirectory ())             . Addjsonfile ("Appsettings.json");            Configuration = Builder. Build ();            Config.dbconn = configuration.getvalue<string> ("Dbconn");            Buildwebhost (args). Run ();        }        public static Iwebhost Buildwebhost (string[] args) =            Webhost.createdefaultbuilder (args)                . Usestartup<startup> ()                . Build ();    }

<2.4> new DataContext Class

Installation reference: MySql.Data.EntityFrameworkCore

    public class Datacontext:dbcontext    {public        dbset<tbuser> tbusers {get; set;}        protected override void Onconfiguring (Dbcontextoptionsbuilder optionsbuilder) = Optionsbuilder.usemysql ( config.dbconn);    }
    public class Tbuser    {        [databasegenerated (Databasegeneratedoption.none)] public        int ID {get; set;}        public string Nickname {get; set;}        public string Email {get; set;}    }

<2.5> New Tbuserdatahandle Class

    public class Tbuserdatahandle:datacontext {public static int adduser (Tbuser user) {try {using (var context = new DataContext ()) {context.                    Database.ensurecreated (); Context.                    ADD (user); Context.                SaveChanges ();            } return 200;            } catch (Exception ex) {return 300; }} public static list<tbuser> Get () {try {using (VAR co                    ntext = new DataContext ()) {var users = context.tbusers;                    list<tbuser> items = new list<tbuser> (); foreach (var item in users) {items.                    ADD (item);                } return items;          }} catch (Exception ex)  {return null; }} public static Tbuser Get (int id) {try {using (var context                    = new DataContext ()) {var u = context.tbusers.Find (id);                return u;            }} catch (Exception ex) {return null; }} public static int Delete (int id) {try {using (var context                    = new DataContext ()) {var u = context.tbusers.Remove (New Tbuser () {id = id}); Context.                    SaveChanges ();                return 200;            }} catch (Exception ex) {return 300; }} public static int Put (int id, tbuser user) {try {using (VA         R context = new DataContext ()) {           var u = context.tbusers.Update (user); Context.                    SaveChanges ();                return 200;            }} catch (Exception ex) {return 300; }        }    }

<2.6> New Usercontroller Api

    [Produces ("Application/json")]    [Route ("Api/user")]    public class Usercontroller:controller    {        //post Api/user        [httppost] public        int POST (tbuser user)        {            return tbuserdatahandle.adduser (user);        }        Get Api/user        [httpget] public        list<tbuser> Get ()        {            return tbuserdatahandle.get ();        }        //GET API/USER/5        [HttpGet ("{ID}")]        Public Tbuser Get (int id)        {            return tbuserdatahandle.get (ID);        }        Delete API/USER/5        [Httpdelete ("{ID}")] public        int Delete (int id)        {            return Tbuserdatahandle.delete (ID);        }        Put API/USER/5        [Httpput ("{ID}")] public        int Put (int id, tbuser user)        {            return Tbuserdatahandle.put (ID, user);        }    }
3. Build Docker image after build and run

To modify DOCKER-COMPOSE.YML, the version of DOCKER-COMPOSE.OVERRIDE.YML is:

Version: ' 2.0 '

The dbconn of the configuration Appsettings.json are:

"Dbconn": "Server=192.168.99.100;user id=root;password=123456;persistsecurityinfo=true;port=3307;database=user; Sslmode=none "

Go to E:\web\ilinkcore (this directory is the root directory for the solution)

Docker-compose up

After successful execution, a ilinkcore image is created and a ilinkcore_ilinkcore_1 container is run, which maps the 32783 port of this machine to port 80 of the container.

4. Test the Access interface

Add reverse proxy, modify Nginx configuration

server{    listen       ;    server_name  localhost;    Location/{          proxy_pass   http://192.168.99.100:32783;          Index  index.html index.htm;      }       }

Re-run Nginx and use Postman for API interface testing

<4.1> Test interface to add user data

Add Items in Headers:

Content-type:application/json

<4.2> View all user data

<4.3> retrieving a user's data

<4.4> update a user's data

Add Items in Headers:

Content-type:application/json

<4.5> Delete individual users

ASP. NET core Webapi uses EF to make a search for MySQL and generate a Docker image build container to run

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.