在EF Core的DbContext中,我们可以通过DbContext或DbSet的Attach方法,来让DbContext上下文来跟踪(track)一个实体对象,假设现在我们有User实体对象,其UserCode为Key属性:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace EFCoreDB.Entities { public partial class User { public User() { UserRole = new HashSet<UserRole>(); } public int Id { get; set; } [Key] public string UserCode { get; set; } public string Username { get; set; } public string Password { get; set; } public string MailAddress { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public <