Best Entity Framework Reference Concise Tutorial




EF Version History and Features links​

EF querying approaches:

## LINQ to Entities:
context.Students.where(s => s.StudentName == “Bill”)
## Entity SQL:
string sqlString = “SELECT VALUE st FROM SchoolDBEntities.Students ” +
                    “AS st WHERE st.StudentName == ‘Bill'”;
var objctx = (ctx as IObjectContextAdapter).ObjectContext;
ObjectQuery<Student> student = objctx.CreateQuery<Student>(sqlString);
## EntityConnection and EntityCommand:
using (var con = new EntityConnection(“name=SchoolDBEntities”))
    EntityCommand cmd = con.CreateCommand();
    cmd.CommandText = “SELECT VALUE st FROM SchoolDBEntities.Students as st where st.StudentName=’Bill'”;
    Dictionary<int, string> dict = new Dictionary<int, string>();
    using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection))
            while (rdr.Read())
                int a = rdr.GetInt32(0);
                var b = rdr.GetString(1);
                dict.Add(a, b);
## Native SQL:
using (var ctx = new SchoolDBEntities())
    var studentName = ctx.Students.SqlQuery(“Select studentid, studentname, standardId from Student where studentname=’Bill'”).FirstOrDefault<Student>();
// extract only a string INSTEAD of a full entity.
var studentName = ctx.Database.SqlQuery<string>(“Select studentName from Students where ID=1”).FirstOrDefault<string>();
LINQ Reference
Leave a Reply

Your email address will not be published. Required fields are marked *