随笔分类 - .net / EntityFramework
摘要:var groupData = context.Students // 使用GroupBy分组,依据ClassId .GroupBy(s => s.ClassId) .Select(g => new { // 拿到ClassId,由于是按ClassId分组的,因此使用Max还是Min得到的一样的 C
阅读全文
摘要:using var transaction = context.Database.BeginTransaction(); try { // 批量添加1 context.AddRange(new List<Student>() { new Student(){ ClassId=3,Name="cc"
阅读全文
摘要:在实体框架Entity Framework Core 中,可以使用 .Include() 方法来加载相关实体的导航属性。.Include() 方法用于在查询中包含指定的导航属性,以便在查询结果中返回相关联的实体数据。通过使用 .Include() 方法,可以避免延迟加载(lazy loading)导
阅读全文
摘要:第一种- 在Fluent API中设置外键 public class Book { /// <summary> /// id /// </summary> public int Id { get; set; } /// <summary> /// 书本名称 /// </summary> public
阅读全文
摘要:前言: 真实入手项目的时候如果向 https://www.cnblogs.com/tlfe/p/18284025 中 第5点这么给每个表中的字段配置,不方便后期维护和管理1. 在根目录创建一个EntityConfig文件夹 2. 根据实体类创建一个BookEntityConfig.cs类文件 nam
阅读全文
摘要:1.创建一个APS.NET MVC项目 2.安装Nuget包 Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools 3.在Mod
阅读全文
摘要:1.安装Nuget包 Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design 2.创建Book,Post两个实体类 public
阅读全文
摘要:1. Update-Database xxxx 把数据库回滚到xxx的状态,迁移脚本不动. 2. Remove-m igration 删除最后一次的迁移脚本 3. Script-Migration 生成迁移SQL代码,有了Update-Database 为什么还要生成SQL, 可以生成版本D到版本F
阅读全文
摘要:安装两个NuGet: Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools 1.创建一个Book.cs文件 using System; using System.Collections.Generic;
阅读全文