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

使用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")
        {
        }

相关资料更多>