首页 > 数据库 >framework4.8 使用sqlsugar

framework4.8 使用sqlsugar

时间:2023-03-02 11:56:25浏览次数:43  
标签:return db System 使用 using framework4.8 public sqlsugar

使用nuget安装mysql

 

 

安装sqlsugar 

 

 

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace sqlsugar_demo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
//建表
var db = ConnectionSqlsugar();
db.Open();

db.Insertable<Student>(new Student()
{
Name = "李白",
SchoolId = 1
}).ExecuteCommand();

return View();
}


public static SqlSugarClient ConnectionSqlsugar()
{

//创建数据库对象
SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "Server=localhost;Port=3308;Database=comcms2;Uid=root;Pwd=123456;",
DbType = DbType.MySql,
IsAutoCloseConnection = true
},
db => {
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql);
};
});

return Db;
}


//实体与数据库结构一样
public class Student
{
//数据是自增需要加上IsIdentity
//数据库是主键需要加上IsPrimaryKey
//注意:要完全和数据库一致2个属性
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public int? SchoolId { get; set; }
public string Name { get; set; }
}


public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}
}

标签:return,db,System,使用,using,framework4.8,public,sqlsugar
From: https://www.cnblogs.com/wugh8726254/p/17171306.html

相关文章