EFCORE基本使用
用 nuget 引用相关包
nuget 搜索 entityframeworkcore 安装标记的这两个
1.创建个类,对应数据库表
/// <summary> /// 账号表 /// </summary> public class Sys_Account { /// <summary> /// 主键 /// </summary> public int Id { get; set; } /// <summary> /// 账号 /// </summary> public string Account { get; set; } /// <summary> /// 密码 /// </summary> public string Password { get; set; } /// <summary> /// 手机号 /// </summary> public string Phone { get; set; } /// <summary> /// 头像 /// </summary> public string Image { get; set; } /// <summary> /// 角色 /// </summary> public ICollection<Sys_Role> Sys_Role { get; set; } /// <summary> /// 创建时间 /// </summary> public DateTime CareateTime { get; set; } public virtual Sys_Shop Sys_Shops { get; set; } }
先弄个DbContext 看看怎么用的
2. 创建连接 DbContextOptionsBuilder配置上下文
1 DbContextOptionsBuilder dob = new DbContextOptionsBuilder();
2 dob.UseSqlServer("Data Source=.;Initial Catalog=Mang.Core.Db;Persist Security Info=True;User ID=sa;Password=password01!");
3. 创建DbContext
1 DbContext db = new DbContext(dob.Options); 2 3 db.Set<Sys_Account>().Add(new Sys_Account()); 4 db.Set<Sys_Account>().Remove(new Sys_Account()); 5 db.Set<Sys_Account>().Where(m => m.Id > 0); 6 db.Set<Sys_Account>().Find(100); 7 db.Set<Sys_Account>().Find("100"); 8 db.Set<Sys_Account>().Any(m => m.Id > 0); 9 db.Set<Sys_Account>().RemoveRange(new Sys_Account(), new Sys_Account()); 10 11 db.Entry<Sys_Account>(new Sys_Account()).State = Microsoft.EntityFrameworkCore.EntityState.Added; 12 db.Entry<Sys_Account>(new Sys_Account()).State = Microsoft.EntityFrameworkCore.EntityState.Modified; 13 db.Entry<Sys_Account>(new Sys_Account()).State = Microsoft.EntityFrameworkCore.EntityState.Detached; 14 db.Entry<Sys_Account>(new Sys_Account()).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; 15 db.Entry<Sys_Account>(new Sys_Account()).State = Microsoft.EntityFrameworkCore.EntityState.Deleted; 16 db.Entry<Sys_Account>(new Sys_Account()).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged;
然后我们继承DbContext,使用构造函数传递DbContextOptionsBuilder配置上下文
/// <summary> /// 框架上下文 /// </summary> public class MangCoreContext : DbContext { public MangCoreContext(DbContextOptions<MangCoreContext> options) : base(options) { } public DbSet<Sys_Shop> Sys_Shops { get; set; } public DbSet<Sys_Account> Sys_Accounts { get; set; } public DbSet<Sys_Role> Sys_Roles { get; set; } }
- 然后 配置注入
配置数据库连接
下面开始调用
1 public class HomeController : Controller 2 { 3 public MangCoreContext _MangCoreContext { get; set; } 4 5 public HomeController(MangCoreContext MangCoreContext) 6 { 7 _MangCoreContext = MangCoreContext; 8 9 } 10 11 public IActionResult Index() 12 { 13 _MangCoreContext.Sys_Accounts.Add(new DbModels.MangCore.MangCoreModels.Sys_Account() 14 { 15 Account = "admin", 16 CareateTime = DateTime.Now, 17 Image = "//", 18 Password = "123456", 19 Phone = "18600912000" 20 }); 21 _MangCoreContext.SaveChanges(); 22 var accounts = _MangCoreContext.Sys_Accounts.Where(m => true).Select(m => new { Sys_Roles = m.Sys_Role.ToList() }).ToList(); 23 24 return View(); 25 } 26 }
基本使用方法就这些,下一篇讲延迟加载
快加群 460845632