(EF4.1 - 4.0 框架)
Web 上的大多数代码示例都规定了 Entity Framework 的最佳实践;他们说将您对 DBContext 的使用包装在 using 块中,以确保无状态操作。即便如此,我还是得到了似乎是共享缓存错误。
错误
ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。
环顾四周,当有人在许多调用中共享 DBContext 的全局实例时,就会出现这种情况。
然而,我在第二次调用以下函数时收到此消息,该函数位于静态数据访问层服务类中。
public static void UpdateRollout(Rollout rollout)
{
using (ITAMEFContext db = new ITAMEFContext(ConnectionStrings.XYZConnectionString))
{
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
FixUp(rollout);
db.Rollouts.Attach(rollout);
db.Entry(rollout).State = System.Data.EntityState.Modified;
db.SaveChanges();
//db.Entry(rollout).State = System.Data.EntityState.Detached;
}
}
private static void FixUp(Rollout rollout)
{
// ensure manual fixup of foreign keys
if (rollout.RolloutState != null)
rollout.FK_RolloutState_ID = rollout.RolloutState.ID;
if (rollout.Lead != null)
rollout.RolloutLead_FK_User_ID = rollout.Lead.ID;
}
EFContext 是通过引用 edmx 模型的 EF 4.x DBContext Fluent Generator 生成的。
看起来像这样。
public partial class ITAMEFContext : DbContext
{
static ITAMEFContext()
{
Database.SetInitializer<ITAMEFContext>(null);
}
public ITAMEFContext() : base("name=ITAMEFContext")
{
this.Configuration.LazyLoadingEnabled = false;
}
public ITAMEFContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
public ITAMEFContext(string nameOrConnectionString, DbCompiledModel model) : base(nameOrConnectionString, model)
{
}
public ITAMEFContext(DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection)
{
}
public ITAMEFContext(DbConnection existingConnection, DbCompiledModel model, bool contextOwnsConnection) : base(existingConnection, model, contextOwnsConnection)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Configurations.Add(new Asset_Mapping());
modelBuilder.Configurations.Add(new AssetAllocation_Mapping());
modelBuilder.Configurations.Add(new AssetAssignee_Mapping());
modelBuilder.Configurations.Add(new AssetAssigneeType_Mapping());
modelBuilder.Configurations.Add(new AssetDeAllocation_Mapping());
modelBuilder.Configurations.Add(new AssetState_Mapping());
modelBuilder.Configurations.Add(new AssetType_Mapping());
modelBuilder.Configurations.Add(new Department_Mapping());
modelBuilder.Configurations.Add(new Location_Mapping());
modelBuilder.Configurations.Add(new ManagementGroup_Mapping());
modelBuilder.Configurations.Add(new Role_Mapping());
modelBuilder.Configurations.Add(new Rollout_Mapping());
modelBuilder.Configurations.Add(new RolloutState_Mapping());
modelBuilder.Configurations.Add(new ServiceArea_Mapping());
modelBuilder.Configurations.Add(new Software_Mapping());
modelBuilder.Configurations.Add(new SoftwareType_Mapping());
modelBuilder.Configurations.Add(new SubTeam_Mapping());
modelBuilder.Configurations.Add(new sys_UserLock_Mapping());
modelBuilder.Configurations.Add(new Team_Mapping());
modelBuilder.Configurations.Add(new User_Mapping());
modelBuilder.Configurations.Add(new WorkingMethod_Mapping());
}
public DbSet<Asset> Assets { get; set; }
public DbSet<AssetAllocation> AssetAllocations { get; set; }
public DbSet<AssetAssignee> AssetAssignees { get; set; }
public DbSet<AssetAssigneeType> AssetAssigneeTypes { get; set; }
public DbSet<AssetDeAllocation> AssetDeAllocations { get; set; }
public DbSet<AssetState> AssetStates { get; set; }
public DbSet<AssetType> AssetTypes { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<ManagementGroup> ManagementGroup { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<ServiceArea> ServiceAreas { get; set; }
public DbSet<SubTeam> SubTeams { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<User> User { get; set; }
public DbSet<WorkingMethod> WorkingMethods { get; set; }
public DbSet<Rollout> Rollouts { get; set; }
public DbSet<RolloutState> RolloutStates { get; set; }
public DbSet<Software> Softwares { get; set; }
public DbSet<SoftwareType> SoftwareTypes { get; set; }
public DbSet<sys_UserLock> sys_UserLock { get; set; }
}
我希望能够根据需要多次从我的 BL 层调用 UpdateRollout。UI 将需要保留作为先前获取的列表的一部分返回的 POCO Rollout 实体图。
Rollout 和所有其他实体都是纯 POCO,不需要上下文跟踪。
我读到一旦使用块处理了 ITAMEFContext,任何上下文缓存/跟踪都会被删除。但是,似乎在同一应用程序域中的任何 DBContext 实例下都有某种全局缓存?老实说,到目前为止,EF 似乎比对分层应用程序使用良好的旧存储过程更有效。
POCO。
public partial class Rollout
{
public Rollout()
{
this.AssetAssignees = new HashSet<AssetAssignee>();
}
public int ID { get; set; }
public string Name { get; set; }
public int RolloutLead_FK_User_ID { get; set; }
public string EmailContacts { get; set; }
public System.DateTime Schedule { get; set; }
public int FK_RolloutState_ID { get; set; }
public Nullable<int> NotificationDays { get; set; }
public string Notes { get; set; }
public virtual ICollection<AssetAssignee> AssetAssignees { get; set; }
public virtual User Lead { get; set; }
public virtual RolloutState RolloutState { get; set; }
}
编辑:
映射。
internal partial class Rollout_Mapping : EntityTypeConfiguration<Rollout>
{
public Rollout_Mapping()
{
this.HasKey(t => t.ID);
this.ToTable("Rollout");
this.Property(t => t.ID).HasColumnName("ID");
this.Property(t => t.Name).HasColumnName("Name").IsRequired().HasMaxLength(50);
this.Property(t => t.RolloutLead_FK_User_ID).HasColumnName("RolloutLead_FK_User_ID");
this.Property(t => t.EmailContacts).HasColumnName("EmailContacts").HasMaxLength(500);
this.Property(t => t.Schedule).HasColumnName("Schedule");
this.Property(t => t.FK_RolloutState_ID).HasColumnName("FK_RolloutState_ID");
this.Property(t => t.NotificationDays).HasColumnName("NotificationDays");
this.Property(t => t.Notes).HasColumnName("Notes");
this.HasRequired(t => t.Lead).WithMany(t => t.Rollouts).HasForeignKey(d => d.RolloutLead_FK_User_ID);
this.HasRequired(t => t.RolloutState).WithMany(t => t.Rollouts).HasForeignKey(d => d.FK_RolloutState_ID);
}
}