asp mvc .net6 sqlsugar 使用

1、Nuget sqlsugarCore 包

2、配置文件 appsettings.json

 "ConnectionStrings": {
   //Sqlserver
   "BaseDbSqlServer": "",
   //Oracle
   "BaseDbOracle": "",
   //MySql
   "BaseDbMySql": ""
 },

3、创建 sqlsugarbase 类

using SqlSugar;

namespace WebApp.Models
{
    public class SqlsugarBase
    {
        /// <summary>
        /// 读取json配置文件
        /// </summary>
        private static IConfigurationRoot Configuration = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
               .Build();
        /// <summary>
        /// 读取配置文件下的连接字符串
        /// </summary>
        string connectionString = Configuration.GetSection("ConnectionStrings").GetSection("BaseDbSqlServer").Value;
        //string connectionString= Configuration.GetConnectionString("BaseDbSqlServer");

        public SqlSugarClient db => GetInstance();
        SqlSugarClient GetInstance()
        {
            var db = new SqlSugarClient(
                new ConnectionConfig
                {
                    ConnectionString = connectionString,
                    DbType = DbType.SqlServer,//数据库类型
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                }
            );
            return db;
        }
    }

}

4、创建 调用类 并继承base类

namespace WebApp.Models
{
    public class Service:SqlsugarBase
    {
        public List<Movie> GetUserList()
        {
            List<Movie> list = new List<Movie>();
            list = db.Queryable<Movie>().ToList();
            return list;
        }
    }
}

5、控制器使用

 Service Service=new Service();
 public IActionResult Index()
 {
     ViewData.Model = Service.GetUserList();
     return View();
 }

6、视图

@{
    ViewData["Title"] = "Home Page";
}

<html>
<head>
    <script type="text/javascript" src="~/Scripts/jquery-3.4.1.js"></script>
</head>
<body>
    <h2>This is a test</h2>
    <h4>Movie</h4>
    <br />
    <table class="table">
        <thead>
            <tr>
                <td>Id</td>
                <td>Title</td>
                <td>ReleaseDate</td>
                <td>Genre</td>
                <td>Price</td>
            </tr>
        </thead>
        <tbody id="content">
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.Id</td>
                    <td>@item.Title</td>
                    <td>@item.ReleaseDate</td>
                    <td>@item.Genre</td>
                    <td>@item.Price</td>
                </tr>
            }
        </tbody>
    </table>
</body>
</html>

7、安装iis

8、.net core 环境包 部署

下载 .NET 6.0 (Linux、macOS 和 Windows)

9、程序发布

10、iis 部署

  • 13
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值