Beispiel #1
0
        static void ExecuteRawSQL()
        {
            using (var ctx = new EFCoreDemoDbContext())
            {
                var students = ctx.Student.FromSqlRaw("SELECT * FROM dbo.t_student WHERE first_name='xi'").ToList();

                var firstName = "xi";
                var students2 = ctx.Student.FromSqlInterpolated($"SELECT * FROM dbo.t_student WHERE first_name={firstName}").ToList();
            }
        }
Beispiel #2
0
        static void ExecuteStoredProcedure()
        {
            //方法一:DbSet<TEntity>.FromSql(),适用于查询
            //方法二:DbContext.Database.ExecuteSqlCommand(),适用于修改

            //查询
            using (var ctx = new EFCoreDemoDbContext())
            {
                var firstName = "xi";
                var students  = ctx.Student.FromSqlInterpolated($"exec sp_GetStudents {firstName}").ToList();
            }

            //修改
            using (var ctx = new EFCoreDemoDbContext())
            {
                var firstName = "xi";
                ctx.Database.ExecuteSqlInterpolated($"exec sp_UpdateStudents {firstName}");
            }
        }