4. Add efcore support and use the mysql database ., Efcoremysql

Source: Internet
Author: User

4. Add efcore support and use the mysql database ., Efcoremysql

1. Add and add

Add in project. json

"tools": {    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"  },  "dependencies": {    "Microsoft.NETCore.App": {      "type": "platform",      "version": "1.0.0"    },    "Pomelo.EntityFrameworkCore.MySql": "1.0.1",    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"  },

Add

"Microsoft. EntityFrameworkCore. Tools": "1.0.0-preview2-final"

Add dependencies
"Pomelo. EntityFrameworkCore. MySql": "1.0.1", "Microsoft. EntityFrameworkCore. Tools": "1.0.0-preview2-final"
Add in Startup. cs
Public void ConfigureServices (IServiceCollection services) {// Add framework services. services. addApplicationInsightsTelemetry (Configuration); services. addDbContext <enter your DbContext> (options => options. useMySql (Configuration. getConnectionString ("DefaultConnectionString"); services. addMvc ();}

You can enter your database connection string in the following section. // For example, "Server = localhost; database = MySqlDemo; uid = root; pwd = 123456 ;"

Services. AddDbContext <enter your DbContext> (options => options. UseMySql (Configuration. GetConnectionString ("DefaultConnectionString ")));
The Configuration here reads appsettings. json.
So you need to add
"ConnectionStrings": {    "DefaultConnectionString": "Server=localhost;database=MySqlDemo;uid=Quan;pwd=123456;"  },

Then he can read this string.

Of course, your DbContext must have a constructor.

 public class MySqlDemoContext : DbContext    {        public MySqlDemoContext(DbContextOptions<MySqlDemoContext> options): base(options)        {        }
}
In this way, your mysql database can be trained. Of course, an error may occur when you connect to and store data in this way, because efcore does not actively create a database.
Public class MySqlDemoInitializer {public static void Seed (IApplicationBuilder app) {// Get an instance of the DbContext from the DI container using (var context = app. applicationServices. getRequiredService <MySqlDemoContext> () {// if the database does not exist, context is created. database. ensureCreated (); // There is no data in the semi-segment in the table. if there is no initialization data, if not, it will initialize if (context. set <Account> (). any () = false) {SetBasicData (context); SetTestAdmin (context); # if DEBUG SetTestNews (context); SetTestCase (context); SetTestPartner (context ); setTestCompany (context); SetTestPosition (context); SetTestProblem (context); SetTestRefund (context); # endif }}}

 

Here is an initialization method. It is not flexible to run this method in startup. cs. It will only be executed once after the program runs. It does not, like ef6, determine the database after running and create a database if it does not exist.

Public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {// put this method to the end and execute MySqlDemoInitializer. Seed (app );}

Another point is that the retrieved Model does not have a foreign key.
We need to use greedy loading to obtain the foreign key associated with the Model.
db.Set<Account>()                .Include(m => m.Role)                .Include(m => m.AccountInfo)                .Include(m => m.AccountModules);

And how to use it in Controller

public class AccountController : Controller{        private AccountService _accountServ;        public AccountController(MySqlDemoContext _db)        {            _accountServ = new AccountService(_db);        }}    

Through dependency injection, the program automatically releases MySqlDemoContext new and injects it in

 

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.