diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Blog.csproj" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Blog.csproj" index 1b28a01c81c7c4d708d59ca52502ae76b6f2760c..b12abd0717d1bd281c7ec33fe1f230ba82436db2 100644 --- "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Blog.csproj" +++ "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Blog.csproj" @@ -6,4 +6,12 @@ enable + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/20241209092204_InitialCreate.Designer.cs" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/20241209092204_InitialCreate.Designer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..0ee4fa917a88c3dc8919925c365c8f17a9caddc0 --- /dev/null +++ "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/20241209092204_InitialCreate.Designer.cs" @@ -0,0 +1,54 @@ +// +using Blog.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Blog.Migrations +{ + [DbContext(typeof(BlogDbContext))] + [Migration("20241209092204_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Blog.Models.Blogs", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Author") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Blogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/20241209092204_InitialCreate.cs" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/20241209092204_InitialCreate.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7a972e5b136462eafd3161caec57ba0e610a68cb --- /dev/null +++ "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/20241209092204_InitialCreate.cs" @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Blog.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Blogs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Title = table.Column(type: "nvarchar(max)", nullable: false), + Content = table.Column(type: "nvarchar(max)", nullable: false), + Author = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Blogs", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Blogs"); + } + } +} diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/BlogDbContextModelSnapshot.cs" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/BlogDbContextModelSnapshot.cs" new file mode 100644 index 0000000000000000000000000000000000000000..69cd6dca0a50b0aa3f4aac04f4b412a2209afe44 --- /dev/null +++ "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Migrations/BlogDbContextModelSnapshot.cs" @@ -0,0 +1,51 @@ +// +using Blog.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Blog.Migrations +{ + [DbContext(typeof(BlogDbContext))] + partial class BlogDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Blog.Models.Blogs", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Author") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Blogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Models/BlogDbContext.cs" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Models/BlogDbContext.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d09f45531e03435f83fff0b2d28ece0d4a348f0b --- /dev/null +++ "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\220\216\344\275\234\344\270\232/MVCdemo/Blog/Models/BlogDbContext.cs" @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; }=null!; + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=.\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} \ No newline at end of file diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/20241209-\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.md" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/20241209-\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..3a25f482b7e60ccbfd5e01a46729ae52fad62ee7 --- /dev/null +++ "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/20241209-\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.md" @@ -0,0 +1,74 @@ +## 数据模型 +1. 数据库的选择和权衡 + - Sqlserver + - PostgreSQL + - Mysql/MariaDb + - Oracle + - Db2 + - Sybase +2. ORM工具的选择和使用 + - EntityFrameworkCore + - Dapper + - SqlSuper + +## 数据的持久化 + - 结绳记事 + - 甲骨文 + - 木简 + - 竹简 + - 纸 + - 账本 + - 文本文件、Excel (磁盘:HDD、SSD) + - 数据库 + - 常见数据库 + - Sqlserver + - PostgreSQL + - Mysql/MariaDb - + - 常见ORM工具(比喻为应用和数据库之间的通讯员) + - Dapper + - EntityFrameworkCore + - FreeSql + +### 应用EntityFrameworkCore的步骤——可以看看官方,还是比较推荐的 +官方网址: + +1. 安装依赖包,命令: +``` +dotnet add package Microsoft.EntityFrameworkCore.SqlServer +``` +2. 定义数据库表模型 +```C# +namespace Blog.Models; + +public class Blogs +{ + public int Id { get; set; } + public string Title { get; set; } = null!; + public string Content { get; set; } = null!; + public string Author { get; set; } = null!; +} +``` +3. 定义数据库上下文 +```C# +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; } = null!; + + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=.\\数据库实例名;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;");//如果本地计算机不被信任,需要加上这个 +} +``` +4. 生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) +5. 将上一步生成的迁移文件,更新到数据库:dotnet ef database update(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) + - 要连接成功:打开任务管理器-->服务-->打开服务-->SQL Server (SQLEXPRESS)-->启动 + + +成功了的话,数据库界面是这样的: +![](./数据库.png) diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/20241212-\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/20241212-\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" new file mode 100644 index 0000000000000000000000000000000000000000..4af9aec342bea5f18794490bfaca97ddbede2c7e --- /dev/null +++ "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/20241212-\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" @@ -0,0 +1,212 @@ +### 今日笔记 +```cs +public dynamic T4(){ + int[] numbers = {1,2,3,4,5,6}; + var res1 = numbers.All(x => x > 2); + //所有的元素都符合条件,返回true;只要有一个不符合,就为false + var res2 = numbers.Any(x => x > 2); + //只要任何一个元素符合条件,返回true;所有都不符合,就为false + return res; +} +``` + +### Linq查询作业 +基础练习 +```cs + public IActionResult One() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + //1.查询特定元素 找出数组中等于5的元素 + var a = numbers.Where(x => x == 5); + //2.查询特定范围的元素 找出数组中在2到8之间的元素 + var b = numbers.Where(x => x >= 2 && x <= 8); + + ViewBag.a = string.Join("",b); + //3.查询并转换元素 将数组中的每个数字乘以2 + var c=numbers.Select(x=>x*2); + return View(); + } + public IActionResult Two() + { + //4.查询特定属性的对象 找出所有名字以"王"开头的学生 + List students = new List + { + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "王中王", Age = 22 }, + new Student {Id=3, Name = "张语嫣", Age = 23 }, + new Student {Id=4, Name = "詹宇航", Age = 35 }, + new Student {Id=5, Name = "郑雨良", Age = 26 }, + }; + var d = students.Where(x => x.Name.StartWith("王")); + return d; + } + public IActionResult Three() + { + List students = new List + { + //5.查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, + }; + var e = students.Where(n=>n.Age>=20).OrderByDescending; + return e; + } + public IActionResult Four() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + //6.查询并去重 找出数组中所有不重复的数字。 + var a=numbers.Distinct(); + //7.查询第一个元素 找出数组中第一个大于3的元素。 + var b=numbers.FirstOrDefault(x=>x>3); + //8.查询最后一个元素 找出数组中最后一个小于7的元素。 + var c=numbers.LastOrDefault(x=>x<7); + //9.查询元素是否存在 检查数组中是否存在大于10的元素 + var d=numbers.Any(x=>x>10); + //10.查询元素的计数 计算数组中大于5的元素数量。 + var e=numbers.Count(x=>x>5); + //11.查询元素的总和 计算数组中所有元素的总和。 + var f=numbers.Sum(); + //12.查询元素的最大值 找出数组中的最大值。 + var g=numbers.Max(); + //13.查询元素的最小值 找出数组中的最小值。 + var h=numbers.Min(); + //14.查询元素的平均值 计算数组中所有元素的平均值。 + var i=numbers.Average(); + //15.查询特定条件的元素 找出数组中能被3整除的元素。 + var j=numbers.Where(n => n % 3 == 0); + return View(); + } +``` + +中级练习 +```cs + public IActionResult One() + { + List students = new List + { + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, + }; + List courses=new List + { + new Course{StudentId=1,CourseName="英语"}, + new Course{StudentId=1,CourseName="数学"}, + new Course{StudentId=2,CourseName="语文"}, + new Course{StudentId=3,CourseName="物理"}, + new Course{StudentId=4,CourseName="化学"}, + new Course{StudentId=4,CourseName="生物"}, + new Course{StudentId=4,CourseName="语文"}, + }; + //16.查询并选择特定属性 找出所有学生的姓名和年龄 + var a = students.Select(x => new {x.Name,x.Age}); + //Age = x.Age+1 这个可以拿来计算农历年龄 + //老胡上课说的,这里也给写上吧。(我就是不想单独整理,怎么了,打死我?) + var res = stujdents.Select(x => { + var tmpName = x.Age <= 22 ? $"{x.Name}小朋友":$"{x.Name}先生";//没错,是的,我就是先生 + var tmpAge = x.Age + 1; + return new {Name = tmpName,Age = tmpAge}; + }); + return res; + //17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + var b=students.GroupBy(s => s.Age) .Select(g => new { Age = g.Key, Count = g.Count()}); + return View(); + //18.查询并联结 联结学生和课程表,找出每个学生的所有课程。 + var a = from student in students join course in courses on student.Id equals course.StudentId into studentCourses + select new{StudentName = student.Name,Courses = studentCourses.Select(c => c.CourseName).ToList()}; + return View(a.ToList()); + } + public IActionResult Three() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + //19.查询并反转 反转数组中的元素顺序。 + var a = numbers.AsEnumerable().Reverse(); + int[] four = a.ToArray(); + //20.查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + int b = numbers.FirstOrDefault(n => n > 2); + if (b != 0) + { + int index = Array.IndexOf(numbers, b); + for (int i = index + 1; i < numbers.Length; i++) + { + numbers[i] = b; + } + } + //21.查询并排除 从数组中排除所有小于5的元素。 + var c = numbers.Where(n => n >= 5); + return View(); + } + public IActionResult Four() + { + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + //22.查询并填充默认值 如果数组中存在null值,用默认值0替换。 + var a = nullableNumbers.Select(n => n ?? 0); + return View(); + } + public IActionResult Five() + { + string[] stringNumbers = { "1", "2", "3", "4" }; + //23.查询并转换类型 将字符串数组转换为整数数组。 + int[] a = stringNumbers.Select(int.Parse).ToArray(); + return View(); + } + public IActionResult Six() + { + object[] objects = { "String", 123, "Another String", 456 }; + var result = objects.OfType(); + //24.查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。 + return View(); + } +``` + +高级练习 +```cs + public IActionResult One() + { + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + //25.查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + var a = numbers1.Zip(numbers2, (first, second) => new { first, second }); + return View(); + } + public IActionResult Two(){ + //26.查询并使用Range生成 生成一个包含1到10的整数数组 + int[] a = Enumerable.Range(1, 10).ToArray(); + return View(a); + } + public IActionResult Three(){ + //27.查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + // 重复的元素 + int number = 7; + // 重复的次数 + int repeatCount = 10; + // 使用LINQ的Repeat方法重复元素 + var a = Enumerable.Repeat(number, repeatCount).ToArray(); + return View(); + } + public IActionResult Four(){ + //28.查询并使用Take限制数量 从数组中取出前5个元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 使用LINQ的Take方法取出前5个元素 + var a = numbers.Take(5).ToArray(); + return View(); + } + public IActionResult Five(){ + //29.查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 使用LINQ的Skip方法跳过前3个元素 + var a = numbers.Skip(3).ToArray(); + return View(); + } +``` \ No newline at end of file diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/image.png" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/image.png" new file mode 100644 index 0000000000000000000000000000000000000000..c9e82b3be60a76c271ab779dbafd58fd96431efd Binary files /dev/null and "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/image.png" differ diff --git "a/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/\346\225\260\346\215\256\345\272\223.png" "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/\346\225\260\346\215\256\345\272\223.png" new file mode 100644 index 0000000000000000000000000000000000000000..2825ca56c0ad7d6c0764b4b6eb14901b3a010324 Binary files /dev/null and "b/\345\274\240\350\257\255\345\253\243/\350\257\276\345\240\202\347\254\224\350\256\260/\346\225\260\346\215\256\345\272\223.png" differ