asp.net mvc – EF Core: Add element to Collection without retrieving full object – Code Utility

[

I have some code looking like this:

Controller:

foreach (Guid id in someModel.Ids)
{
    var someSlowToFetchEntity = await dbContext.SlowClass.FindAsync(id);
    someOtherEntity.AddSlowEntity(someSlowToFetchEntity)
}

Model someOtherEntity:

private ObservableCollection<SlowClass> _SlowEntity = new();
public ObservableCollection<SlowClass> SlowEntity
{
    get
    {
        return __lazyLoader.Load(this, ref _SlowEntity);
    }
    set
    {
        _SlowEntity = value;
    }
}

public SlowClass AddSlowEntity(SlowClass slowClass)
{
    SlowEntity.Add(slowClass);
    return slowClass;
}

SlowClass:

namespace SomeDataModel {
    public class SlowClass : Entity {

        public static SlowClass Create(IEntityCreationContext ecc) {
            return new SlowClass(ecc);
        }

        private string _Name;
        public string Name {
            get {
                return _Name;
            }
            set {
                if (_Name != value) {
                    NotifyPropertyChanging();
                    _Name = value;
                    NotifyPropertyChanged();
                }
            }
        }  
        
        // This is potentailly huge(up to 50MB allowed)
        private byte[] _Content;
        public byte[] Content
        {
            get {
                return _Content;
            }
            set {
                if (_Content != value) {
                    NotifyPropertyChanging();
                    _Content = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public override void LoadDefaults(DbContext dbc) {
            base.LoadDefaults(dbc);
        }
    }


    class _SlowClassConfiguration : _EntityWithMetaDataConfiguration, IEntityTypeConfiguration<Attachment> {
        public void Configure(EntityTypeBuilder<SlowClass> builder) {
            base.Configure<SlowClass>(builder);

            builder.Property(x => x.Name);
            builder.Property(x => x.Content);
        }
    }

}

DBSets:

        public DbSet<SomeOtherEntity> SomeOtherEntity { get; set; }
        public DbSet<SlowClass> SlowClass { get; set; }

The problem is finding and retrieving the slow entity takes roughly 30s (because it’s a big entity, a few MBs big).
EF Core in the end only needs to save the id of the slow entity, is there any way, to save the id directly without fetching the slowEntity first?

,

See if this is helpful: Efficient Updating – EF Core

Here, it’s talking about updating a large number of records, but the same advice may apply to your case:

Unfortunately, EF doesn’t currently provide APIs for performing bulk
updates. Until these are introduced, you can use raw SQL to perform
the operation where performance is sensitive:

context.Database.ExecuteSqlRaw("UPDATE [Employees] SET [Salary] = [Salary] + 1000");

So, when EF Core doesn’t have an efficient solution, you can fall back on SQL.

]