Skip to content

Instantly share code, notes, and snippets.

@A-Programmer
Created March 21, 2017 17:48
Show Gist options
  • Save A-Programmer/317c76d3730cc8c163394353b104b4e8 to your computer and use it in GitHub Desktop.
Save A-Programmer/317c76d3730cc8c163394353b104b4e8 to your computer and use it in GitHub Desktop.
Run SQL query with Entity Framework
//Select query
//Table name : MyTable
//In this table i have one field called : PropertyName
using(var db = new MyDbContext())
{
var myData = db.MyTableName.SqlQuery("SELECT * From MyTableName").ToList();
foreach(var data in myData)
{
Response.Write(data.PropertyName + "<br/>");
}
}
//Insert query
//Table name : MyTable
//In this table i have one field called PropertyName
using(var db = new MyDbContext())
{
db.Database.ExecuteSqlCommand("Insert INTO MyTable (PropertyName) VALUES('MyValue')");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment