日期:2014-05-20 浏览次数:21213 次
private static void QueryContactsUseEntitySQL()
{
using (var context = new SampleEntities())
{
var queryString = "SELECT VALUE c " +
"FROM SampleEntities.Contacts AS c " +
"WHERE c.FirstName='Robert'";
ObjectQuery<Contact> contacts = context.CreateQuery<Contact>(queryString);
foreach (var contact in contacts)
{
Console.WriteLine("{0} {1}", contact.FirstName.Trim(), contact.LastName);
}
}
Console.Write("Press Enter...");
Console.ReadLine();
}
namespace Chapter2ConsoleApp
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class SampleEntities : DbContext
{
public SampleEntities()
: base("name=SampleEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Address> Addresses { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<vOfficeAddresses> vOfficeAddresses { get; set; }
}
}