efCore+Mysql+Net Core
2024-09-02 06:52:47
1.首先新建一个空的Asp.net core项目
2.新建一个类 gj.cs
public class gj
{
// <summary>
/// 主键
/// </summary>
public int id { get; set; } /// <summary>
/// 标题
/// </summary>
public string method { get; set; }
/// <summary>
/// 内容
/// </summary>
public string text { get; set; } public string type { get; set; }
}
3.添加数据库上下文类。
public class DbGj:DbContext
{
public DbSet<gj> gj { set; get; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseMySQL(@"Server=localhost;database=testapp;uid=root;pwd=woshishui");
}
4.添加控制器
public class GJController : Controller
{ public IActionResult Index()
{
using (var db = new DbGj())
{
var lis = db.Set<gj>().ToList();
return View(lis);
} } }
5.视图
@model IEnumerable<CoreTest_1.Models.gj> @{
ViewData["Title"] = "Index";
} <h1>Index</h1> <p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.method)
</th>
<th>
@Html.DisplayNameFor(model => model.text)
</th>
<th>
@Html.DisplayNameFor(model => model.type)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.method)
</td>
@*<td>
@Html.DisplayFor(modelItem => item.text)
</td>*@
<td>
@Html.DisplayFor(modelItem => item.type)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
F5运行程序 (如图)
最新文章
- J2EE、J2SE、J2ME是什么意思?
- HTTP协议学习---(九)cookie
- Linq操作
- OC3-xml文件解析
- Python学习笔记整理(四)Python中的字符串..
- node.js介绍
- Gcc 命令大全
- 百度ueditor的图片上传,前后端交互使用
- python的文件读写笔记
- 一文让你彻底理解 Java NIO 核心组件
- 进程间通信IPC-信号
- MT【181】横穿四象限
- poj3436 ACM Computer Factory, 最大流,输出路径
- [原]unity3d GLSL无法pc上显示
- python 继承与多重继承
- 基于HTML5 Canvas粒子效果文字动画特效
- Scrum立会报告+燃尽图 03
- DSP5509的中断学习-第4篇
- 在u-boot中添加命令
- springmvc使用实体參数和ServletAPI