Created
March 21, 2017 17:48
-
-
Save A-Programmer/317c76d3730cc8c163394353b104b4e8 to your computer and use it in GitHub Desktop.
Run SQL query with Entity Framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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