/// <summary> /// 上端 /// </summary> /// <param name="args"></param> static void Main(string[] args) { try { using (LcDbContext context = new LcDbContext()) { { //context.Database.EnsureDeleted(); //context.Database.EnsureCreated(); } { //var Addcompany = new Company() //{ // CompanyName = "朝夕教育", // CreateTime = DateTime.Now, // CreatorId = 1, // Description = "专注于培养新一代C#.Net技术精英", // LastModifyTime = DateTime.Now, // LastModifierId = 1 //}; //context.Conmpany.Add(Addcompany); //context.SaveChanges(); //Company company = context.Conmpany.Find(1); //var companyList = context.Conmpany.Where(c => c.Id > 1); } { //EFCore支持Linq查询; QueryTest.Show(); } { //NavigationTest.Show(); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public static void Show() { #region 其他查询 using (LcDbContext dbContext = new LcDbContext()) { { //var list = dbContext.SysUser.Where(u => 1 == 1 && !(new int[] { 1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 14, 17 }.Contains(u.Id)));//in查询 //foreach (var user in list) //{ // System.Console.WriteLine(user.UserName); //} } { //var list = from u in dbContext.SysUser // where new int[] { 1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 14, 17 }.Contains(u.Id) // select u; //foreach (var user in list) //{ // System.Console.WriteLine(user.UserName); //} } { //var list = dbContext.SysUser.Where(u => new int[] { 1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 14, 17 }.Contains(u.Id)) // .OrderBy(u => u.Id) //排序 // .Select(u => new //投影 // { // Name = u.Name, // Pwd = u.Password // }).Skip(3).Take(5); //跳过三条 再获取5条 //foreach (var user in list) //{ // System.Console.WriteLine(user.Name); //} } { //var list = dbContext.SysUser.Where(u => u.Name.StartsWith("Richard") && u.Name.EndsWith("老师")) // .Where(u => u.Name.EndsWith("老师")) // .Where(u => u.Name.Contains("Richard")) // .Where(u => u.Name.Length < 10) // .OrderBy(u => u.Id); //foreach (var user in list) //{ // System.Console.WriteLine(user.Name); //} } { //var list = from u in dbContext.SysUser // join c in dbContext.SysUserRoleMapping on u.Id equals c.SysUserId // where new int[] { 1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 14, 17 }.Contains(u.Id) // select new // { // Name = u.Name, // Pwd = u.Password, // RoleId = c.SysRoleId // }; //foreach (var user in list) //{ // System.Console.WriteLine("{0} {1}", user.Name, user.Pwd); //} } { //var list = from u in dbContext.SysUser // join m in dbContext.SysUserRoleMapping on u.Id equals m.SysUserId // join r in dbContext.SysRole on m.SysRoleId equals r.Id // where new int[] { 1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 14, 17 }.Contains(u.Id) // select new // { // Name = u.Name, // Pwd = u.Password, // RoleId = m.SysRoleId, // RoleName = r.Name // }; //foreach (var user in list) //{ // System.Console.WriteLine("{0} {1} {2}", user.Name, user.Pwd, user.RoleName); //} } } using (LcDbContext dbContext = new LcDbContext()) { { try { string sql = "Update dbo.SysUser Set Password='******' WHERE Id=@Id"; SqlParameter parameter = new SqlParameter("@Id", 1); int flag = dbContext.Database.ExecuteSqlRaw(sql, parameter); } catch (Exception ex) { throw ex; } } } //TransactionScope 在EFCore 里面 用不了。。。 框架现在还不支持。。后续应该会支持。。。 using (LcDbContext dbContext = new LcDbContext()) { { //事务 IDbContextTransaction trans = null; try { trans = dbContext.Database.BeginTransaction(); string sql = "Update dbo.SysUser Set Password='******' WHERE Id=@Id"; SqlParameter parameter = new SqlParameter("@Id", 10); dbContext.Database.ExecuteSqlRaw(sql, parameter); trans.Commit(); } catch (Exception ex) { if (trans != null) { trans.Rollback(); } throw ex; } finally { trans.Dispose(); } } } #endregion }
public static void Show() { using (LcDbContext context = new LcDbContext()) { { /////可以一次添加复杂对象 //var company = new Company() //{ // Name = "朝夕教育", // CreateTime = DateTime.Now, // CreatorId = 1, // Description = "专注于培养新一代C#.Net技术精英", // LastModifyTime = DateTime.Now, // LastModifierId = 1, // SysUser = new List<SysUser>() { // new SysUser() { // Name = "Richard 老师", // Mobile = "18672713698", // CreateTime = DateTime.Now, // Password="******" // }, // new SysUser() { // Name = "Eleven 老师", // Mobile = "18672713698", // CreateTime = DateTime.Now, // Password="******" // }, // new SysUser() { // Name = "Apple老师", // Mobile = "18672713698", // CreateTime = DateTime.Now, // Password="******" // } // } //}; //context.Conmpany.Add(company); //context.SaveChanges(); } { ////在EFCore 里面默认是不加载导航属性的数据; //List<Company> companyList = context.Conmpany.Include(c=>c.SysUser).ToList(); //// Include ////List<Company> companyList1 = context.Conmpany.Include(c => c.SysUser).ToList(); //foreach (Company company in companyList) //{ // var userList = company.SysUser; // foreach (var user in userList) // { // var cmpy = user.Company; // foreach (var item in cmpy.SysUser) //无限循环的 // { // } // } //} } { //context.SysLog.Add(new SysLog() //{ // LogType = new byte(), // UserName = "******", // SysLogDetail = new SysLogDetail() // { // CreateTime = DateTime.Now, // Introduction = "架构班的VIP课程", // CreatorId = 1, // LastModifierId = 1, // LastModifyTime = DateTime.Now // } //}); //context.SaveChanges(); //SysLog sysLog= context.SysLog.FirstOrDefault(); //SysLogDetail sysLogDetail= context.SysLogDetail.FirstOrDefault(); } { ////Include:预先加载 ////ThenInclude: 多个层架的预先加载 //Company company = context.Conmpany.Include(c => c.SysUser).FirstOrDefault(); //foreach (SysUser user in company.SysUser) //{ // Company company1 = user.Company; // foreach (var item in company1.SysUser) // { // } //} } // //{ // Company company = context.Conmpany.Include(c => c.SysUser).FirstOrDefault(); // foreach (SysUser user in company.SysUser) // { // Company company1 = user.Company; // foreach (var item in company1.SysUser) // { // } // } // Company company2 = context.Conmpany.FirstOrDefault(); // foreach (SysUser user in company2.SysUser) // { // Company company3 = user.Company; // foreach (var item in company3.SysUser) // { // } // } //} } }