织梦模板网站好优化吗,兰州市网络设计方案,用thinkcmf做的网站,江苏建设官方网站EFCore官方文档置顶#xff0c;看这个就行。下面的内容只是总结#xff0c;算是备忘录。 一、创建和删除
//1、创建数据库和表
db.Database.EnsureCreated();//将创建数据库#xff08;如果不存在#xff09;并初始化数据库架构。 如果存在任何表 (包括另一 DbContext 类)…EFCore官方文档置顶看这个就行。下面的内容只是总结算是备忘录。 一、创建和删除
//1、创建数据库和表
db.Database.EnsureCreated();//将创建数据库如果不存在并初始化数据库架构。 如果存在任何表 (包括另一 DbContext 类) 的表则不会初始化架构。//仅在书中没有表时有效
//2、删除数据库
db.Database.EnsureDeleted//方法将删除数据库如果存在。 如果没有适当的权限则会引发异常。
//3、迁移数据库
context.Database.Migrate();
//4、初始化数据库和表。可以用来创建不存在的表已存在表则会报错
//EnsureCreated 仅在数据库中没有表时有效。 如果需要可以编写自己的检查来查看架构是否需要初始化并使用基础 IRelationalDatabaseCreator 服务初始化架构。
var databaseCreator dbContext.GetServiceIRelationalDatabaseCreator();
databaseCreator.CreateTables();二、表模型
[Table(Books)]//命名表名
public class Book
{[Key]//主键。Id或type nameId将被配置为实体主键。其他属性名则需要配置[Key][Column(book_Id)]//表的类名public long Id { get; set; }[Column(TypeName varchar(200),Order 1/*列顺序*/)][Comment(The URL of the blog)]//列注释public string? Title { get; set; }[Column(TypeName datetime2(7))][Precision(7)]//与datetime2(7)等价public DateTime? PubTime { get; set; }[Column(TypeName decimal(5,2))]//精度5小数位2[Precision(5, 2)]//与decimal(5,2)等价public double? Price { get; set; }[MaxLength(500)]//与varchar(500)等价public string? Author { get; set; }
}三、DbContext
public class TestDbContext : DbContext
{//表格实体public DbSetBook Books { get; set; }public string DbPath { get; }public TestDbContext(){string desktopSystem.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);DbPath Path.Join(desktop, TestDB.db);}//连接数据库字符串protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlite($Data Source{DbPath});}protected override void OnModelCreating(ModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);modelBuilder.EntityBook().Property(x x.Price).HasDefaultValue(1.00);//列设置默认属性modelBuilder.EntityBook().Property(x x.PubTime).HasDefaultValue(DateTime.Now);//列设置默认属性}
}四、查询。Linq表达式
//1、加载所有数据
var books context.Books.ToList();
//2、加载单个对象
var book context.Books.Single(b b.Id 1);
//3、筛选
var books context.Books.Where(b b.Url.Contains(dotnet)).ToList();
//4、跟踪查询。查询到的对象可以用来修改数据库中的值var firstdb.list.First();
first.Name已修改;
context.SaveChanges();
//5、非跟踪查询。查询到的对象无法修改数据库中的值
var firstdb.list.AsNoTracking().First();
first.Name已修改;
db.SaveChanges();
//6、非跟踪查询2。与上面等效多次操作时可以少用几个AsNoTracking()方法
db.ChangeTracker.QueryTrackingBehaviorQueryTrackingBehavior.NoTracking;
var firstdb.list.First();
first.Name已修改;
db.SaveChanges();
//7、非跟踪查询3。在连接字符串后面增加限定
protected override void OnConfiguring(DbContextOptionsBuilder options) options.UseSqlite($Data Source{DbPath}).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
//8、分组和统计
//得到的group是List匿名类。匿名类的属性就2个ClassId 和StudentCount
var group db.Students//使用GroupBy分组依据ClassId.GroupBy(s s.ClassId).Select(g new{//拿到ClassId由于ClassId是分组的因此使用Max或Min是一样的ClassId g.Max(s s.ClassId),//使用Count函数拿到总数StudentCount g.Count()});五、删除
//1、批量删除
db.list.Where(xx.Id30).ExecuteDelete();
//2、单个删除
var studb.list.FirstOrDefault();
db.list.Remove(stu);
int deleteCountdb.SaveChanges();六、添加
//1、单个添加
db.Add(new Student(){ClassId2,Name赵子龙});
db.SaveChanges();
//2、多个添加
db.AddRange(new ListStudent(){new Student(){ClassId2,Name张翼德}),new Student(){ClassId2,Name关云长}),});
db.SaveChanges();七、修改
//1、单个修改
var stu db.list.FirstOrDefault(x x.Name.Equals(赵子龙));
stu.Name 赵云;
db.Entry(stu).State EntityState.Modified;
int editCount db.SaveChanges();
//2、批量修改1
var stus db.list.Where(x x.ClassId2);
foreach (var stu in stus)
{stu.Name 赵云;db.Entry(stu).State EntityState.Modified;
}
int editCount db.SaveChanges();
//3、批量修改2。与上面方法效果一致
db.list.Where(x x.ClassId2).ExecuteUpdate(ss.SetProperty(bb.Name,赵云));八、操作
//SaveChange()可以同时处理多个操作。不明确、不批量、不知道时使用该方法
//ExecuteDelete()方法会立即执行。明确、批量、知道时使用该方法
//ExecuteUpdate ()方法会立即执行。明确、批量、知道时使用该方法九、事务
//事务允许以院子方式处理多个数据库操作。如果已提交事务则所有操作都会成功应用到数据库。如果已回滚事务则所有操作都不会应用到数据库。
using var transaction db.Database.BeginTransaction();
try
{//批量添加1db.AddRange(new ListPost(){new Post(){ PostId3,Titlecc },new Post(){ PostId3,Titledd },});db.SaveChanges();//批量添加2db.AddRange(new ListPost(){new Post(){ PostId3,Titleee },new Post(){ PostId3,Titleff },});db.SaveChanges();//提交事务transaction.Commit();
}
catch (Exception ex)//提交事务会出现异常
{//回滚事务transaction.Rollback();Console.WriteLine(ex.Message);
}
文章转载自: http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.mldrd.cn.gov.cn.mldrd.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.kxnjg.cn.gov.cn.kxnjg.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.nmymn.cn.gov.cn.nmymn.cn http://www.morning.jksgy.cn.gov.cn.jksgy.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.qbwmz.cn.gov.cn.qbwmz.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.cbqqz.cn.gov.cn.cbqqz.cn http://www.morning.rbktw.cn.gov.cn.rbktw.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.rgrz.cn.gov.cn.rgrz.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.ydzly.cn.gov.cn.ydzly.cn http://www.morning.ghfrb.cn.gov.cn.ghfrb.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.xnqjs.cn.gov.cn.xnqjs.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.brhxd.cn.gov.cn.brhxd.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn