ÕâÁ½ÌìÔÚ×ÅʵÑо¿EF CoreÏîÄ¿µ±ÖжÔÓÚһЩ²éѯҲûʵ¼ÊÈ¥¼ì²â£¬ÓÚÊÇÏë×ÅÀûÓ÷żÙʱ¼äȥʵ¼Ê²âÊÔÏ£¬½á¹û±¾ÎľͳöÀ´ÁË£¬too young,too simple£¬ºóÐø²©Ö÷»á´Óµ×²ã·Òë±í´ïʽÊ÷ŪÆð£¬À´´ÓÔ´Í·Á˽âEF Core£¬Í¨¹ý±¾ÎÄÄã»áÃ÷°×²»ÊÇEF CoreÍŶÓû×öÐÔÄÜÓÅ»¯£¬¶øÊÇÄã¸ù±¾¾ÍûÓùý¶øÇÒÕýÔÚµ¹ÍË¡£
EntityFramework CoreÐÔÄÜÓÅ»¯³õ̽¼òµ¥´Ö±©Ö±½ÓÉÏ´úÂ룬¸ø³öÉÏÏÂÎÄÒÔ¼°ÐèÒªÓõ½µÄ²âÊÔÀ࣬ÈçÏ£º
public class EFCoreContext : DbContext { public DbSet<Blog> Blogs { get; set; } OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer(); OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>(pc => { pc.ToTable().HasKey(k => k.Id); pc.Property(p => p.Name).IsRequired(); pc.Property(p => p.Url).IsRequired(); pc.Property(p => p.Count).IsRequired(); pc.Property(p => p.RowVersion).IsRequired().IsRowVersion().ValueGeneratedOnAddOrUpdate(); }); } }
ÄãÊÇ·ñÏñÈçÏÂÈ¥»ñÈ¡·ÖÒ³Êý¾ÝÄØ£¬ÎÒÃÇÀ´Ò»ÆðÇÆÇÆ£º
var ef = new EFCoreContext(); var blogs = ef.Blogs; var example1 = blogs .Skip(1) .Take(1) .ToList(); var example2 = blogs .Skip(10) .Take(10) .ToList();
ÎÒÃÇͨ¹ýÈçÏÂSQLÓï¾äÀ´²é¿´²éѯ¼Æ»®Éú³ÉµÄSQLÓï¾ä£º
SELECT sys.syscacheobjects.cacheobjtype, sys.dm_exec_query_stats.execution_count, sys.syscacheobjects.SQL, sys.dm_exec_query_plan.query_plan FROM sys.dm_exec_query_stats INNER JOIN sys.dm_exec_cached_plans ON sys.dm_exec_cached_plans.plan_handle = sys.dm_exec_query_stats.plan_handle INNER JOIN sys.syscacheobjects ON sys.syscacheobjects.bucketid = sys.dm_exec_cached_plans.bucketid CROSS APPLY sys.dm_exec_query_plan(sys.dm_exec_query_stats.plan_handle)
½á¹ûÈçÏ£º
ÎÒÃÇÔÙÀ´¿´¿´xmlÎļþÖÐÉú³ÉµÄSQLÓï¾äÊÇÔõÑùµÄ¡£
Õâ˵Ã÷ʲôÎÊÌâÄØ£¬ÉÏÊö²éѯ¼Æ»®ÖÐÉú³ÉµÄSQLÓï¾ä¶ÔÓÚÎÒÃÇÉÏÊöȥȡÊý¾ÝÊ×Ñ¡´ÓµÚ¶þÌõÈ¡Ò»Ìõ£¬½ÓÏÂÀ´ÊÇȥȡµÚÊ®ÌõºóµÄÊ®Ìõ£¬Í¬Ê±ÉÏÊöSQLÓï¾ä¶øÊÇÉùÃ÷ÁËÁ½¸ö±äÁ¿£¬»»ÑÔÖ®£¬ÉÏÊöÁ½ÌõÓï¾ä²éѯ×îÖÕÔÚµÚÒ»´Î²éѯºóSQL²éѯ¼Æ»®½øÐÐÁË»º´æ£¬Ï´ÎÔÙȥȡÊý¾Ýʱֱ½Óµ÷ÓôËSQLÓï¾äÒÔ´Ë´ïµ½ÖØÓõÄÄ¿µÄ£¬ÏÂÃæÒªÊÇÎÒÃǽøÐÐÈçϸÄÔ죬½á¹û»áÔõÑùÄØ£¿
var ef = new EFCoreContext(); var blogs = ef.Blogs; var count = 1; var example1 = blogs .Skip(count) .Take(count) .ToList(); count = 10; var example2 = blogs .Skip(count) .Take(count) .ToList();
½á¹û¾¹ýÉÏÊö¸ÄÔìÀûÓñäÁ¿µÄÐÎʽºÍÖ±½Ó¸³ÖµµÄÐÎʽÊÇÒ»Öµģ¬Ã»ÓÐʲô¿É½²µÄ£¬ÏÂÃæÎÒÃÇÔÙÀ´½²ÊöÁíÍâÒ»ÖÖÇé¿ö¡£Çë¼ÌÐøÍùÏ¿´¡£
var ef = new EFCoreContext(); var blogs = ef.Blogs; var skipTakeWithInt1 = blogs .OrderBy(b => b.Id).Where(d => d.Name.Length > 1).ToList(); var skipTakeWithInt2 = blogs .OrderBy(b => b.Id).Where(d => d.Name.Length > 10).ToList();
¿´³öʲôûÓУ¬¶ÔÓÚÉÏÊöÁ½Ìõ²éѯÔòÊǶÔÓ¦½øÐÐÁËÁ½´ÎSQL²éѯ£¬ÕâÏÂÒâʶµ½ÁËÆäÖÐÐþ»úÁË°É£¬ÏÂÃæÎÒÃǽ«ÉÏÊöÔÙ¸ÄÔìһϣº
var ef = new EFCoreContext(); var blogs = ef.Blogs; var length = 1; var skipTakeWithVariable1 = blogs .OrderBy(b => b.Id).Where(d => d.Name.Length > length).ToList(); length = 10; var skipTakeWithVariable2 = blogs .OrderBy(b => b.Id).Where(d => d.Name.Length > length).ToList();
¡¡