entity framework DbContext Attach判断 DbEntityEntry<T> entry = db.Entry<T>(model); if (entry.State == EntityState.Detached) db.Set<T>().Attach(model);