Include&Entity Framework 6创建重复记录

时间:2018-10-25 20:57:26

标签: entity-framework-6 asp.net-core-webapi

当前,我正在使用ASP.net核心以及EF6。但是,当我尝试包含相关数据时(我已关闭了延迟加载)。然后进行保存更改,它会复制我的数据。

我目前正在使用.AsNoTracking(),但是当有孩子对它如何工作有任何想法时,它没有影响?

这是我的上下文的一些配置:

        this.context.Configuration.LazyLoadingEnabled = false;
        this.context.Configuration.AutoDetectChangesEnabled = true;
        this.context.Database.CommandTimeout = 180;

我建立了IQueryable,然后发送该消息,然后附加.AsNoTracking这样的代码(我正在使用Chris Pratt的出色的通用仓库:https://cpratt.co/truly-generic-repository/

    protected virtual IQueryable<TEntity> GetQueryable<TEntity>(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = null,
        int? skip = null,
        int? take = null)
        where TEntity : class, IEntity
    {
        includeProperties = includeProperties ?? string.Empty;
        IQueryable<TEntity> query = context.Set<TEntity>();

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            query = orderBy(query);
        }

        if (skip.HasValue)
        {
            query = query.Skip(skip.Value);
        }

        if (take.HasValue)
        {
            query = query.Take(take.Value);
        }

        return query.AsNoTracking();
    }

0 个答案:

没有答案