切换导航
{{systemName}}
{{ info.Title }}
{{info.Title}}
{{ menu.Title }}
{{menu.Title}}
登录
|
退出
搜索
.net core 使用EF
作者:ych
#### 引用库 ``` Install-package Microsoft.EntityFrameworkCore.SqlServer ``` #### 一.普通模式 ##### 创建model ``` [Table("Live_admin")] public class Live_admin { public int Id { get { return id; } set { id = value; } } public string Uid { get { return uid; } set { uid = value; } } public string Pwd { get { return pwd; } set { pwd = value; } } } ``` ##### appsetting.json ``` { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "ConnectionStrings": { "TestConnection": "Data Source=127.0.0.1;Database=BPM;User ID=root;Password=123456;pooling=true;CharSet=utf8;port=3306;sslmode=none", "Redis": "127.0.0.1:6379" }, "Appsettings": { "SystemName": "JIYUWU .NET CORE6.0", "Date": "2019-06-28", "Author": "Chang" }, "ServiceUrl": "https://www.jiyuwu.com" } ``` ##### ConfigHelper.cs ``` public class ConfigHelper { public static IConfiguration Configuration { get; set; } static ConfigHelper() { //ReloadOnChange = true 当appsettings.json被修改时重新加载 Configuration = new ConfigurationBuilder() .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true }) .Build(); } } ``` ##### 创建MyDBContext ``` public class MyDBContext : DbContext { ///
/// 配置连接字符串 ///
///
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); optionsBuilder.UseSqlServer(ConfigHelper.GetSectionValue("TestConnection")); } public DbSet
Live_admins { get; set; } } ``` ##### 创建实现Live_adminService ``` public async Task
> GetListAsync(string username,string password) { //using (MyDBContext db = new MyDBContext()) //{ // var live_admin = (from e in db.Live_admins // select e).ToList(); // return live_admin; //} using (MyDBContext db = new MyDBContext()) { List
s = new List
(); SqlParameter[] sqlparms = new SqlParameter[2]; sqlparms[0] = new SqlParameter("@username", username); sqlparms[1] = new SqlParameter("@password", password); var result = await db.Database.ExecuteSqlCommandAsync("exec selectTable @ID,@name", sqlparms); var aaa=db.Database.sq foreach (var c in result) { s.Add(new myTable { detail = c.detail, ID = c.ID, mes = c.mes, myTableID = c.myTableID, name = c.name }); } return s; } } ```
相关推荐
post数据过多导致 net::ERR_CONNECTION_RESET
.net core 中使用redis
.NET Core下使用Autofac实现构造函数注入
.net core微软自带的构造函数注入
评论区
先去登录
版权所有:机遇屋在线 Copyright © 2021-2025 jiyuwu Co., Ltd.
鲁ICP备16042261号-1