Database Reference
In-Depth Information
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().ToTable("Chapter3.Student");
}
}
To execute a SQL statement and get back a collection of instances of the Student entity type, follow the pattern
in Listing 3-7.
Listing 3-7. Using ExecuteStoreQuery() to Execute a SQL statement and Get Back Objects
using (var context = new EFRecipesEntities())
{
// delete previous test data
context.Database.ExecuteSqlCommand("delete from chapter3.student");
// insert student data
context.Students.Add (new Student
{
FirstName = "Robert",
LastName = "Smith",
Degree = "Masters"
});
context.Students.Add (new Student
{
FirstName = "Julia",
LastName = "Kerns", Degree = "Masters"
});
context.Students.Add (new Student
{
FirstName = "Nancy",
LastName = "Stiles", Degree = "Doctorate"
});
context.SaveChanges();}
using (var context = new EFRecipesEntities())
{
string sql = "select * from Chapter3.Student where Degree = @Major";
var parameters = new DbParameter[] {
new SqlParameter {ParameterName = "Major", Value = "Masters"}};
var students = context.Students.SqlQuery(sql, parameters);
Console.WriteLine("Students...");
foreach (var student in students)
{
Console.WriteLine("{0} {1} is working on a {2} degree",
student.FirstName, student.LastName, student.Degree);
}
}
 
Search WWH ::




Custom Search