dbcontext mysql_九、.net core用orm继承DbContext(数据库上下文)方式操作数据库
发布日期:2021-06-24 13:48:35 浏览次数:2 分类:技术文章

本文共 3404 字,大约阅读时间需要 11 分钟。

一、创建一个DataContext普通类继承DbContext

6922fd6cafb6012fc0138b4e33ab882e.png

安装程序集:Pomelo.EntityFrameworkCore.MySql

二、配置连接字符串(MySql/SqlServer都可以)

d7139987c25dd9098a24456e77013a2c.png

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 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 list = context.friends.ToList();

return Content(list.ToString());

e4cb4b748a285a3048e3d699121bfeda.png

四、数据库表对应的结构

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 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(options =>

options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

services.AddMvc();

}

注意:鼠标放上去继续点击引用

d011e31897419fa0b5d833b2fb98cf76.png

4、控制器实现代码

DataContext context = new DataContext();

List list = context.friends.ToList();

return Content(list.ToString());

08ab1647870c1e8c356d835ea9e3803a.png

05f2556388aaadf3fe6f244e5a98c977.png

我们点击运行吧 我们发现还是不可用。

我们换一种方式运行

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 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 });

}

}

}

0007a40afe66367d91be316df12aae1b.png

asp.netCore连接多个数据库参考:

3d9685acf675dc4aafbe91de07b48023.png

转载地址:https://blog.csdn.net/weixin_33245447/article/details/114493067 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java bll dal_DAL层与BLL层的设计原则
下一篇:python数值区间处理_Python 数值区间处理_对interval 库的快速入门详解

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月30日 17时17分30秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章