Exemple #1
0
 public void Query_results_not_cached_if_NotCached_used()
 {
     using (var ctx = new MyContext())
     {
         var q = ctx.Entities.Where(e => e.Flag != null).OrderBy(e => e.Id).NotCached();
         Assert.True(BlacklistedQueriesRegistrar.Instance.IsQueryBlacklisted(
             ((IObjectContextAdapter)ctx).ObjectContext.MetadataWorkspace, q.ToString()));
         q.ToList();
         Assert.False(Cache.CacheDictionary.Keys.Any(k => k.Contains(q.ToString())));
     }
 }
Exemple #2
0
 public void Query_results_cached_even_if_Cached_used_on_blacklisted_query()
 {
     using (var ctx = new MyContext())
     {
         var q = ctx.Entities.Where(e => e.Flag == true).NotCached().Cached();
         Assert.True(AlwaysCachedQueriesRegistrar.Instance.IsQueryCached(
             ((IObjectContextAdapter)ctx).ObjectContext.MetadataWorkspace, q.ToString()));
         q.ToList();
         Assert.True(Cache.CacheDictionary.Keys.Any(k => k.Contains(q.ToString())));
     }
 }
Exemple #3
0
 public void Query_results_cached_if_Cached_used_on_query_with_side_effects()
 {
     using (var ctx = new MyContext())
     {
         var q = ctx.Entities.Where(e => e.Name == Guid.NewGuid().ToString()).Cached();
         Assert.True(AlwaysCachedQueriesRegistrar.Instance.IsQueryCached(
             ((IObjectContextAdapter)ctx).ObjectContext.MetadataWorkspace, q.ToString()));
         q.ToList();
         Assert.True(Cache.CacheDictionary.Keys.Any(k => k.Contains(q.ToString())));
     }
 }
Exemple #4
0
 public void DbContext_GetCachingProviderServices_returns_correct_value()
 {
     using (var ctx = new MyContext())
     {
         Assert.NotNull(ctx.GetCachingProviderServices());
     }
 }
Exemple #5
0
 public void ObjectContext_GetCachingProviderServices_returns_correct_value()
 {
     using (var ctx = new MyContext())
     {
         Assert.NotNull(((IObjectContextAdapter) ctx).ObjectContext.GetCachingProviderServices());
     }
 }
Exemple #6
0
 public void Can_read_null_values()
 {
     using (var trx = new TransactionScope())
     {
         using (var ctx = new MyContext())
         {
             ctx.Entities.Add(new Entity());
             ctx.SaveChanges();
             var e = ctx.Entities.First();
             Assert.Null(e.Name);
             Assert.Null(e.Flag);
         }
     }
 }
Exemple #7
0
        public void CUD_mapped_to_sprocs_reset_cache()
        {
            const string cachedItemKey =
                "EFCache.MyContext_SELECT TOP (1) \r\n    [c].[Id] AS [Id], \r\n    [c].[Data] AS [Data]\r\n    FROM [dbo].[EntitiesMappedToSprocs] AS [c]_";

            using (var ctx = new MyContext())
            {
                ctx.Database.ExecuteSqlCommand("INSERT INTO EntitiesMappedToSprocs VALUES(42)");
                var entity = ctx.EntitiesMappedToSprocs.FirstOrDefault();
                ctx.Entry(entity).State = EntityState.Modified;

                Assert.True(Cache.CacheDictionary.ContainsKey(cachedItemKey));

                ctx.SaveChanges();

                Assert.False(Cache.CacheDictionary.ContainsKey(cachedItemKey));
            }
        }
Exemple #8
0
        public void Can_manually_add_query_to_blacklisted_queries()
        {
            using (var ctx = new MyContext())
            {
                const string query = @"SELECT
            [GroupBy1].[A1] AS [C1]
            FROM ( SELECT
            COUNT(1) AS [A1]
            FROM [dbo].[Entities] AS [Extent1]
            WHERE [Extent1].[Flag] IS NOT NULL
            )  AS [GroupBy1]";

                BlacklistedQueriesRegistrar.Instance.AddBlacklistedQuery(
                    ((IObjectContextAdapter)ctx).ObjectContext.MetadataWorkspace, query);

                ctx.Entities.Count(e => e.Flag != null);
                Assert.False(Cache.CacheDictionary.Keys.Any(k => k.Contains(query.ToString())));
            }
        }
Exemple #9
0
        public void Cache_not_cleared_on_transaction_rollback()
        {
            Cache.PutItem("s", new object(), new[] { "Item", "Entity" }, new TimeSpan(), new DateTime());

            using (var ctx = new MyContext())
            {
                using(var entityConnection = ((IObjectContextAdapter)ctx).ObjectContext.Connection)
                {
                    entityConnection.Open();
                    var trx = entityConnection.BeginTransaction();

                    ctx.Entities.Add(new Entity());
                    ctx.Items.Add(new Item { Id = Guid.NewGuid() });

                    ctx.SaveChanges();

                    Assert.True(Cache.CacheDictionary.ContainsKey("s"));

                    trx.Rollback();

                    Assert.True(Cache.CacheDictionary.ContainsKey("s"));
                }
            }
        }
Exemple #10
0
        public void Cache_cleared_on_implicit_transaction_commit()
        {
            Cache.PutItem("s", new object(), new[] {"Item", "Entity"}, new TimeSpan(), new DateTime());

            using (var ctx = new MyContext())
            {
                ctx.Entities.Add(new Entity());
                ctx.Items.Add(new Item { Id = Guid.NewGuid()});

                ctx.SaveChanges();

                Assert.False(Cache.CacheDictionary.ContainsKey("s"));
            }
        }
Exemple #11
0
        public void Cache_cleared_on_explicit_transaction_commit_Async()
        {
            Cache.PutItem("s", new object(), new[] { "Item", "Entity" }, new TimeSpan(), new DateTime());

            using (var ctx = new MyContext())
            {
                using (var entityConnection = ((IObjectContextAdapter)ctx).ObjectContext.Connection)
                {
                    entityConnection.Open();
                    var trx = entityConnection.BeginTransaction();

                    ctx.Entities.Add(new Entity());
                    ctx.Items.Add(new Item { Id = Guid.NewGuid() });

                    var x = ctx.SaveChangesAsync().GetAwaiter().GetResult();

                    Assert.True(Cache.CacheDictionary.ContainsKey("s"));

                    trx.Commit();

                    Assert.False(Cache.CacheDictionary.ContainsKey("s"));
                }
            }
        }
Exemple #12
0
        public void Cached_data_returned_from_cache_Async()
        {
            using (var ctx = new MyContext())
            {
                var id = 3;
                var q = ctx.Entities.Where(e => e.Id == id);
                q.ToListAsync().GetAwaiter().GetResult();

                Assert.True(Cache.CacheDictionary.ContainsKey("EFCache.MyContext_" + q + "_p__linq__0=3"));
            }
        }