【问题标题】:EF Core: Add element to Collection without retrieving full objectEF Core:将元素添加到集合而不检索完整对象
【发布时间】:2021-09-21 17:33:49
【问题描述】:

我有一些看起来像这样的代码:

控制器:

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

建模 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;
}

慢班:

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);
        }
    }

}

数据库集:

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

问题是查找和检索慢速实体大约需要 30 秒(因为它是一个大实体,有几 MB 大)。 EF Core到底只需要保存slowEntity的id,有什么办法,直接保存id而不先取slowEntity?

【问题讨论】:

  • 你能展示一下你的 SlowEntity 和 Slow 类吗,关系部分好吗?我不明白你想达到什么目的
  • @Serge 我更正了命名,我想这让你感到困惑,同时我正在添加我的 SlowClass
  • 谢谢,我还是看不懂关系。你使用我从未见过的语法。你能发布像这个公共虚拟 DbSet Slow { get; 这样的 dbset 列表吗?放; } 等等
  • 完成了,有帮助吗?始终如一地为演示重命名某些东西很难,再次修复它。再次查看控制器:)

标签: asp.net-mvc entity-framework-core


【解决方案1】:

看看这是否有帮助:Efficient Updating - EF Core

这里说的是更新大量记录,但同样的建议可能适用于您的情况:

很遗憾,EF 目前不提供用于执行批量操作的 API 更新。在引入这些之前,您可以使用原始 SQL 来执行 性能敏感的操作:

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

因此,当 EF Core 没有有效的解决方案时,您可以求助于 SQL。

【讨论】:

  • 这听起来对我有帮助,非常感谢,几天后会标记你的答案。如果没有其他人进来:)
猜你喜欢
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多