zoukankan      html  css  js  c++  java
  • EntityFramework 学习 一 Execute Native SQL Query

    SQL query for entity types:

    using (var ctx = new SchoolDBEntities())
    {
        var studentList = ctx.Students.SqlQuery("Select * from Student").ToList<Student>();
      
    }
         

    注意:查询的字段必须和实体中的属性名对应

    SQL query for non-entity types:

    using (var ctx = new SchoolDBEntities())
    {
        //Get student name of string type
        string studentName = ctx.Database.SqlQuery<string>("Select studentname 
            from Student where studentid=1").FirstOrDefault<string>();
    }

    Raw SQL commands to the database:

    using (var ctx = new SchoolDBEntities())
    {
    
        //Update command
        int noOfRowUpdated = ctx.Database.ExecuteSqlCommand("Update student 
                set studentname ='changed student by command' where studentid=1");
        //Insert command
        int noOfRowInserted = ctx.Database.ExecuteSqlCommand("insert into student(studentname) 
                values('New Student')");
        //Delete command
        int noOfRowDeleted = ctx.Database.ExecuteSqlCommand("delete from student 
                where studentid=1");
    
    }
  • 相关阅读:
    2019nc#7
    ABC133F
    2019DX#6
    2019DX#5
    2019dx#4
    解决一般图最大匹配——带花树算法
    2019nc#4
    B-generator 1_2019牛客暑期多校训练营(第五场)
    hdu-6638 Snowy Smile
    hdu-6621 K-th Closest Distance
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6623117.html
Copyright © 2011-2022 走看看