日期:2014-05-20  浏览次数:21228 次

使用VS2012创建了一个控制台程序,添加ADO.NET Entity Data Model,但是提示CreateQuery<Contact>方法不存在!
代码如下:
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; }
    }
}


已经引用了using System.Data.Objects;命名空间,请问如何解决,谢谢!
------解决方案--------------------
ObjectQuery本来就没有这个方法,只有CreateContext才有CreateQuery。