Linq强类型的datacontext将无法编译

时间:2011-03-05 19:49:51

标签: linq-to-sql datacontext

我正在尝试使用强类型的LINQ To SQL datacontext从SQL Server表中获取所有数据,根据此MSDN示例:How to: Connect to a Database (LINQ to SQL)

我的代码如下:

    public class bioAppointments
    {
        string _strConnString = "Data Source=local; Initial Catalog=MyDatabase; User Id=MyUser; Password=MyPassword;";

        public List<Appointment> getAllAppointments()
        {

            SqlConnection connection = new SqlConnection(_strConnString);

            Appt dc = new Appt(connection);
            var query =
                from appt in dc.myAppointments
                select new Appointment
                {
                    AppointmentId = appt.AppointmentId,
                    StartDate = appt.StartDate,
                    EndDate = appt.EndDate,
                    StartTime = appt.StartTime,
                    EndTime = appt.EndTime,
                    Notes = appt.Notes,
                };

            return query.ToList();
        }

        public partial class Appt : DataContext
        {
            public Table<Appointments> myAppointments;
            public Appt(string connection) : base(connection) { }
        }

        public class Appointment
        {
            public int AppointmentId { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public DateTime StartTime { get; set; }
            public DateTime EndTime { get; set; }
            public String Notes { get; set; }
        }
    }

我收到编译错误:

  

找不到类型或命名空间名称“Appointments”(您是否缺少using指令或程序集引用?)

Appt 类中,波浪线位于 下。

有谁知道这是什么意思以及如何解决它?

1 个答案:

答案 0 :(得分:2)

而不是:

   public Table<Appointments> myAppointments;

不应该是这个吗? (奇异)

   public Table<Appointment> myAppointments;