Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
/ ef-core-bulk Public archive

EF Core bulk operations - INSERT SELECT, UPDATE SELECT , DELETE SELECT

License

Notifications You must be signed in to change notification settings

neurospeech/ef-core-bulk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EF-Core-Bulk

EF Core bulk operations - INSERT SELECT, UPDATE SELECT , DELETE SELECT

            using (var db = this.CreateContext()) {
            
                Seed(db);

                var googleProducts = db.Products.Where(p => p.Name.StartsWith("google"));


                // Copy all products whose name starts with google.
                // effective SQL is 
                // INSERT INTO Products (Name)
                //    (SELECT P.Name + 'V2' as Name FROM Products as P Where P.Name LIKE 'Google%' )
                int count = await db.InsertAsync(googleProducts.Select(p => new Product {
                    Name = p.Name + " V2"
                }));

                Assert.Equal(2, count);

                count = await db.Products.CountAsync();

                Assert.Equal(6, count);

                // Archive all products whose name starts with google
                // effective SQL is
                // UPDATE P SET P.Archived = 1 FROM Products as P WHERE P.Name LIKE 'Google%'
                //
                count = await db.UpdateAsync(googleProducts.Select(p => new Product {
                    Archived = true
                }));

                Assert.Equal(4, count);

                count = await db.Products.Where(x => x.Archived == true).CountAsync();

                Assert.Equal(4, count);

                count = await db.DeleteAsync(db.Products.Where(x => x.Archived == true));

                Assert.Equal(4, count);

                count = await db.Products.Where(x => x.Archived == true).CountAsync();

                Assert.Equal(0, count);

                count = await db.Products.CountAsync();

                Assert.Equal(2, count);
            }
            
         void Seed(AppDbContext db) {
             db.Products.AddRange(new Product {
                Name = "Apple iPhone"
            },
            new Product
            {
                Name = "Google Pixel"
            },
            new Product
            {
                Name = "Microsoft Surface"
            }, 
            new Product {
                Name = "Google Pixel Large"
            });

            db.SaveChanges();

        }

Releases

No releases published

Packages

No packages published

Languages