一、創建一個DataContext普通類繼承DbContext

安裝程序集:Pomelo.EntityFrameworkCore.MySql
二、配置連接字符串(MySql/SqlServer都可以)

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DotNetCore.Models
{
public class DataContext:DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//配置MySql連接字符串/SqlServer連接字符串皆可
optionsBuilder.UseMySql("Server=47.94.174.85;Database=testDb; User=testDb;Password=testDb;");
}
public class t_testModel
{
public int id { get; set; }
public string name { get; set; }
public string pass { get; set; }
}
//添加表實體
public DbSet<t_testModel> friends { get; set; }
}
}
這里注意:
optionsBuilder.UseMySql("Server=47.94.174.85;Database=testDb; User=testDb;Password=testDb;");//的你要安裝Pomelo.EntityFrameworkCore.MySql
optionsBuilder.UseMySQL("Server=47.94.174.85;Database=testDb; User=testDb;Password=testDb;");//的你要安裝MySql.Data.EntityFrameworkCore 這個UserMYSQL中MYSQL是大寫的
三、在控制器里面寫查詢操作
DataContext context = new DataContext();
List<t_testModel> list = context.friends.ToList();
return Content(list.ToString());

四、數據庫表對應的結構
DROP TABLE IF EXISTS `friends`;
CREATE TABLE `friends` (
`id` int(3) NOT NULL,
`name` varchar(8) NOT NULL,
`pass` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `friends` VALUES ('4', '王六', 'dasd');
總結所作的操作
1、創建DataContext類繼承DbContext(一個類文件)
2、控制器里面寫查詢操作
四、數據連接屬性應該存放在appsettings.json內
1、更改DbContext類內容如下所示
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DotNetCore.Models
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions options)
: base(options)
{
}
public class t_testModel
{
public int id { get; set; }
public string name { get; set; }
public string pass { get; set; }
}
//添加表實體
public DbSet<t_testModel> friends { get; set; }
}
}
2、appsettings.json文件更改如下
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=47.94.174.85;Database=testDb; User=testDb;Password=testDb;"
}
}
3、將Startup類里面的ConfigureServices替換成下列代碼
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
注意:鼠標放上去繼續點擊引用

4、控制器實現代碼
DataContext context = new DataContext();
List<t_testModel> list = context.friends.ToList();
return Content(list.ToString());


我們點擊運行吧 我們發現還是不可用。
我們換一種方式運行
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using DotNetCore.Models;
using static DotNetCore.Models.DataContext;
namespace DotNetCore.Controllers
{
public class HomeController : Controller
{
public readonly DataContext _context;
//構造函數,依賴注入數據庫上下文就可以了
public HomeController(DataContext context)
{
_context = context;
}
public IActionResult Index()
{
List<t_testModel> list = _context.friends.ToList();
return Content(list.ToString());
return View();
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
asp.netCore連接多個數據庫參考:

