Пример #1
0
        public void Add(string name, int age)
        {
            var nameAge = new NameAge {
                Name = name, Age = age
            };

            _ctx?.dbRows?.Add(nameAge);
            int?rows = _ctx?.SaveChanges();

            Console.WriteLine($"NameAgeService added {rows} row(s)");
        }
Пример #2
0
        // await NewDbAsync();// 当表不存在的时候,可以重新建表。
        // await RemoveDbAsync();
        // alter table nameage drop constraint PK_NameAge
        // set IDENTITY_INSERT NameAge ON
        // await AddRecordAsync("efcore11", 23); // TODO, no record inserted.

        static async Task QueryUpdateDeleteBooks()
        {
            using (var ctx = new NameAgeContext())
            {
                // tested
                List <NameAge> list = await ctx.DbRows.ToListAsync();

                foreach (var na in list)
                {
                    Console.WriteLine($"{na.NameAgeId}, {na.Name}, {na.Age}");
                }

                Console.WriteLine();

                // tested
                List <NameAge> list2 = await ctx.DbRows
                                       .Where(r => r.Name == "duke").ToListAsync();

                foreach (var na in list2)
                {
                    Console.WriteLine($"{na.NameAgeId}, {na.Name}, {na.Age}");
                }

                // tested
                NameAge record = await ctx.DbRows
                                 .Where(r => r.Name == "selves").FirstOrDefaultAsync();

                if (record != null)
                {
                    record.Age += 1;
                    // record.Name = "selves";
                    int c = await ctx.SaveChangesAsync(); // update statement

                    Console.WriteLine($"{c} records updated");
                }

                // tested
                var toRemove = await ctx.DbRows
                               .Where(r => r.Name == "myname").ToListAsync();

                ctx.DbRows?.RemoveRange(toRemove);
                int count = await ctx.SaveChangesAsync();

                Console.WriteLine($"{count} records deleted");
            }
            //  QueryUpdateDeleteBooks();
        }
Пример #3
0
        public static async Task AddRecordAsync(string name, int age)
        {
            using (var ctx = new NameAgeContext())
            {
                using (var tx = ctx.Database.BeginTransaction())
                {
                    // ctx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.NameAge ON");
                    // ctx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.NameAge OFF");
                    var nameAge = new NameAge {
                        Name = name, Age = age
                    };
                    /*await*/ ctx.DbRows?.Add(nameAge);
                    int rows = await ctx.SaveChangesAsync();

                    // ctx.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.NameAge OFF");
                    Console.WriteLine($"{rows} row(s) inserted");

                    tx.Commit();
                }
            }
        }