当前位置: 移动技术网 > IT编程>开发语言>.net > efCore+Mysql+Net Core

efCore+Mysql+Net Core

2019年08月20日  | 移动技术网IT编程  | 我要评论

绫木口,染发剂怎么用,yoee

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运行程序 (如图)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网