efCore+Mysql+Net Core
程序员文章站
2022-04-14 16:09:40
1.首先新建一个空的Asp.net core项目 2.新建一个类 gj.cs 3.添加数据库上下文类。 4.添加控制器 5.视图 F5运行程序 (如图) ......
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运行程序 (如图)
推荐阅读