Execute raw SQL query in ASP.NET MVC, database first mode

前端 未结 1 910
轻奢々
轻奢々 2021-01-12 11:52

The model of my project is database first, and uses remote access to database on another server. I need to use raw SQL query because my query is very complex and I feel more

相关标签:
1条回答
  • 2021-01-12 12:57

    The Entity Framework Code First API includes methods that enable you to pass SQL commands directly to the database. You have the following options:

    • Use the DbSet.SqlQuery method for queries that return entity types. The returned objects must be of the type expected by the DbSet object, and they are automatically tracked by the database context unless you turn tracking off. (See the following section about the AsNoTracking method.)

    • Use the Database.SqlQuery method for queries that return types that aren't entities. The returned data isn't tracked by the database context, even if you use this method to retrieve entity types.

    • Use the Database.ExecuteSqlCommand for non-query commands.


    Calling a Query that Returns Entities:

    public async Task<ActionResult> Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
    
        // Commenting out original code to show how to use a raw SQL query.
        //Department department = await db.Departments.FindAsync(id);
    
        // Create and execute raw SQL query.
        string query = "SELECT * FROM Department WHERE DepartmentID = @p0";
        Department department = await db.Departments.SqlQuery(query, id).SingleOrDefaultAsync();
    
        if (department == null)
        {
            return HttpNotFound();
        }
        return View(department);
    }
    

    Calling a Query that Returns Other Types of Objects:

    public ActionResult About()
    {
        //Commenting out LINQ to show how to do the same thing in SQL.
        //IQueryable<EnrollmentDateGroup> = from student in db.Students
        //           group student by student.EnrollmentDate into dateGroup
        //           select new EnrollmentDateGroup()
        //           {
        //               EnrollmentDate = dateGroup.Key,
        //               StudentCount = dateGroup.Count()
        //           };
    
        // SQL version of the above LINQ code.
        string query = "SELECT EnrollmentDate, COUNT(*) AS StudentCount "
            + "FROM Person "
            + "WHERE Discriminator = 'Student' "
            + "GROUP BY EnrollmentDate";
        IEnumerable<EnrollmentDateGroup> data = db.Database.SqlQuery<EnrollmentDateGroup>(query);
    
        return View(data.ToList());
    }
    

    Calling an Update Query:

    [HttpPost]
    public ActionResult UpdateCourseCredits(int? credit)
    {
        if (credit != null)
        {
            ViewBag.RowsAffected = db.Database.ExecuteSqlCommand(
                "UPDATE Course SET Credits = Credits * {0}", credit);
        }
        return View();
    }
    

    For more information have a look at Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application (12 of 12). Hope this helps.

    0 讨论(0)
提交回复
热议问题