原文地址:https://www.cnblogs.com/lwqlun/p/10576443.html
作者:Lamond Lu
源代碼:https://github.com/lamondlu/EFCoreFindSample

背景介紹
當我們在工作單元(UnitOfWork)中使用EF/EF Core的時候,為了要保持事務,一個用戶操作只能調用一次SaveChange方法,但是有時候一個用戶操作需要調用多個Repository,並且他們操作的實體是關聯的。這時候在一個Repository中獲取另外一個Repository中添加/修改/刪除的實體就變成了一個問題。
問題說明
當前我們做一個學生管理系統,學生和班之間是多對多關系,一個學生可以屬於多個班, 因此我們創建了如下的EF上下文。
public class TestDbContext : DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Group> Groups { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentGroup>().HasKey(p => new { p.GroupId, p.StudentId });
base.OnModelCreating(modelBuilder);
}
}
[Table("Student")]
public class Student
{
public Student()
{
StudentGroups = new List<StudentGroup>();
}
[Key]
public Guid StudentId { get; set; }
public string Name { get; set; }
public int Credits { get; set; }
public virtual ICollection<StudentGroup> StudentGroups { get; set; }
}
[Table("Group")]
public class Group
{
[Key]
public Guid GroupId { get; set; }
public string GroupName { get; set; }
}
[Table("StudentGroup")]
public class StudentGroup
{
public Guid StudentId { get; set; }
public Guid GroupId { get; set; }
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
[ForeignKey("GroupId")]
public virtual Group Group { get; set; }
}
在用戶界面上,我們允許用戶在添加學生的時候,同時將學生分配到一個班級中。
因此我們的控制器代碼如下:
public class StudentController : ControllerBase
{
private StudentManager _studentManager = null;
public StudentController(StudentManager studentManager)
{
_studentManager = studentManager;
}
// GET api/values
[HttpPost]
public IActionResult Post([FromBody]AddStudentDTO dto)
{
try
{
_studentManager.AddStudent(dto.Name, dto.GroupId);
return StatusCode(201);
}
catch
{
return StatusCode(500, new { message = "Unexpected Issue." });
}
}
}
為了完成我們的業務,在StudentManager的AddStudent方法中,我們需要完成兩步操作
- 添加學生信息
- 將學生分配給指定班
public class StudentManager
{
private IUnitOfWork _unitOfWork;
public StudentManager(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public void AddStudent(string studentName, Guid groupId)
{
var newStudentId = Guid.NewGuid();
_unitOfWork.StudentRepository.AddStudent(newStudentId, studentName);
_unitOfWork.GroupRepository.AssignStudentToGroup(newStudentId, groupId);
_unitOfWork.Commit();
}
}
這里我們使用StudentRepository的AddStudent方法來完成保存學生信息,使用GroupRepository的AssignStudentToGroup方法來將學生分配給班級。
這里,其實不應該將保存學生信息和分配班級都放在這里,可以使用事件發布/訂閱將其分配班級的邏輯移動到別處。
針對保存學生信息的操作,代碼很簡單。
public class StudentRepository : IStudentRepository
{
private TestDbContext _dbContext;
public StudentRepository(TestDbContext dbContext)
{
_dbContext = dbContext;
}
public void AddStudent(Guid studentId, string name)
{
_dbContext.Students.Add(new Student
{
StudentId = studentId,
Name = name,
Credits = 0
});
}
}
但是當我們繼續編寫AssignStudentToGroup方法時就會遇到問題,我們該如何獲取到前面方法中添加的Student實體?
這時候,有同學會去嘗試
_dbContext.Students.Where(p=>p.StudentId = studentId)
你會發現它獲取不到你想要的對象,原因是這條語句進行的是數據庫查詢,當前新增的Student對象還沒有保存到數據庫
那么如何解決這個問題呢?這里有2種解決方案
- 從
ChangeTracker上獲取 - 使用
Find方法獲取
從ChangeTracker上獲取
ChangeTracker是EF/EF Core中的核心對象,在這個對象中記錄了當前EF上下文,操作過的所有實體,實體狀態及實體屬性的變更。
ChangeTracker中的Entries
泛型方法可以幫助我們獲取到當前上下文中操作過的指定類型實體集合。
public void AssignStudentToGroup(Guid studentId, Guid groupId)
{
Student student = _dbContext.ChangeTracker.Entries<Student>().FirstOrDefault(p => p.Entity.StudentId == studentId).Entity;;
if (student == null)
{
throw new KeyNotFoundException("The student id could not be found.");
}
student.StudentGroups.Add(new StudentGroup
{
StudentId = studentId,
GroupId = groupId
});
}
但是這樣寫會出現一個問題,如果我想為一個數據庫中已經存在的學生分配班級,調用這個方法就會出現問題,因為該實體還未加載到ChangeTracker中, 所以我們這里還需要使用_dbContext.Students.First方法進行數據庫查詢.
public void AssignStudentToGroup(Guid studentId, Guid groupId)
{
Student student;
if (_dbContext.ChangeTracker.Entries<Student>().Any(p => p.Entity.StudentId == studentId))
{
student = _dbContext.ChangeTracker.Entries<Student>().First(p => p.Entity.StudentId == studentId).Entity;
}
else if (_dbContext.Students.Any(p => p.StudentId == studentId))
{
student = _dbContext.Students.First(p => p.StudentId == studentId);
}
else
{
throw new KeyNotFoundException("The student id could not be found.");
}
student.StudentGroups.Add(new StudentGroup
{
StudentId = studentId,
GroupId = groupId
});
}
至此,整個方法的修改就完成了。如果你覺着這種方式比較繁瑣,請繼續看下面的Find方法。
使用Find方法
EF/EF Core中其實還提供了一個Find方法,以下是該方法的方法簽名。
// Summary:
// Finds an entity with the given primary key values. If an entity with the given
// primary key values is being tracked by the context, then it is returned immediately
// without making a request to the database. Otherwise, a query is made to the database
// for an entity with the given primary key values and this entity, if found, is
// attached to the context and returned. If no entity is found, then null is returned.
//
// Parameters:
// keyValues:
// The values of the primary key for the entity to be found.
//
// Returns:
// The entity found, or null.
public virtual TEntity Find([CanBeNullAttribute] params object[] keyValues);
從這個Find方法的注釋中,我們可以了解到,Find方法可以根據實體主鍵查詢實體。但是它的優點是,它會優先去ChangeTracker中查找,如果查找不到才會生成查詢語句,進行數據庫查詢。
由此,我們可以使用Find方法修改AssignStudentToGroup方法,看起來比之前的代碼簡化了不少
public void AssignStudentToGroup(Guid studentId, Guid groupId)
{
Student student = _dbContext.Students.Find(studentId);
if (student == null)
{
throw new KeyNotFoundException("The student id could not be found.");
}
student.StudentGroups.Add(new StudentGroup
{
StudentId = studentId,
GroupId = groupId
});
}
