Global setting for AsNoTracking()?

前端 未结 6 1596
礼貌的吻别
礼貌的吻别 2020-11-27 17:30

Originally I believed that

context.Configuration.AutoDetectChangesEnabled = false;

would disable change tracking. But no. Cur

6条回答
  •  死守一世寂寞
    2020-11-27 17:49

    In my case since I needed the whole context to be readonly rather than Read/Write.

    So I did a change to the tt file, and changed all the DbContext properties to return DbQuery instead of DbSet, removed the sets from all properties, and for the gets, I returned the Model.AsNoTracking()

    For example:

    public virtual DbQuery Campaigns { get{ return Set().AsNoTracking();} }

    The way I did this in the tt template is:

    public string DbQuery(EntitySet entitySet)
        {
            return string.Format(
                CultureInfo.InvariantCulture,
                "{0} virtual DbQuery<{1}> {2} {{ get{{ return Set<{1}>().AsNoTracking();}} }}",
                Accessibility.ForReadOnlyProperty(entitySet),
                _typeMapper.GetTypeName(entitySet.ElementType),
                _code.Escape(entitySet));
        }

提交回复
热议问题