简体   繁体   中英

C# LINQ to SQL Invalid Object Name

public class AMCOMDB : DataContext
{
        public Table<student> allStudents;
        public Table<aClass> allClasses;
        public AMCOMDB(string connection) : base(connection) { } 
}

[Table(Name = "student")]
public class student
{
        private string _studentName;

        [Column(IsPrimaryKey =true,Storage ="_studentName")]
        public string studentName
        {
            get { return this._studentName; }
            set { this._studentName = value; }
        }

        private string _LARType;

        [Column(Storage ="_LARType")]
        public string LARType
        {
            get { return this._LARType; }
            set { this._LARType = value; }
        }

        private string _studentType;

        [Column(Storage = "_studentType")]
        public string studentType
        {
            get { return this._studentType; }
            set { this._studentType = value; }
        }

        private string _aviationLevel;

        [Column(Storage = "_aviationLevel")]
        public string aviationLevel
        {
            get { return this._aviationLevel; } 
            set { this._aviationLevel = value; }
        }
        private string _airDefenseLevel;
        [Column(Storage = "_airDefenseLevel")]
        public string airDefenseLevel
        {
            get
            {
                return this._airDefenseLevel;
            }
            set
            {
                this._airDefenseLevel = value;
            }
        }
        private string _emergencyContact;
        [Column(Storage = "_emergencyContact")]
        public string emergencyContact
        {
            get
            {
                return this._emergencyContact;
            }
            set
            {
                this._emergencyContact = value;
            }
        }
[Table(Name = "grades")]
    public class grades
    {
        private string _studentName;
        [Column(IsPrimaryKey = true, Storage = "_studentName")]
        public string studentName
        {
            get
            {
                return this._studentName;
            }
            set
            {
                this._studentName = value;
            }
        }
        private int _ET;
        [Column(Storage = "_ET")]
        public int ET
        {
            get
            {
                return this._ET;
            }
            set
            {
                this._ET = value;
            }
        }
        private int _CP;
        [Column(Storage = "_CP")]
        public int CP
        {
            get
            {
                return this._CP;
            }
            set
            {
                this._CP = value;
            }
        }
        private int _SB;
        [Column(Storage = "_SB")]
        public int SB
        {
            get
            {
                return this._SB;
            }
            set
            {
                this._SB = value;
            }
        }
        private int _EC;
        [Column(Storage = "_EC")]
        public int EC
        {
            get
            {
                return this._EC;
            }
            set
            {
                this._EC = value;
            }
        }
        private int _finalGrade;
        [Column(Storage = "_finalGrade")]
        public int finalGrade
        {
            get
            {
                return this._finalGrade;
            }
            set
            {
                this._finalGrade = value;
            }
        }
       }
[Table(Name = "classes")]
    public class aClass
    {
        private string _classNumber;
        [Column(IsPrimaryKey = true, Storage = "_classNumber")]
        public string classNumber
        {
            get
            {
                return this._classNumber;
            }
            set
            {
                this._classNumber = value;
            }
        }
        private string _courseSeries;
        [Column(Storage = "_courseSeries")]
        public string courseSeries
        {
            get
            {
                return this._courseSeries;
            }
            set
            {
                this._courseSeries = value;
            }
        }
        private string _courseNumber;
        [Column(Storage = "_courseNumber")]
        public string courseNumber
        {
            get
            {
                return this._courseNumber;
            }
            set
            {
                this._courseNumber = value;
            }
        }
        private string _distanceLearning;
        [Column(Storage = "_distanceLearning")]
        public string distanceLearning
        {
            get
            {
                return this._distanceLearning;
            }
            set
            {
                this._distanceLearning = value;
            }
        }
        private string _classStartDate;
        [Column(Storage = "_classStartDate")]
        public string classStartDate
        {
            get
            {
                return this._classStartDate;
            }
            set
            {
                this._classStartDate = value;
            }
        }
        private string _classEndDate;
        [Column(Storage = "_classEndDate")]
        public string classEndDate
        {
            get
            {
                return this._classEndDate;
            }
            set
            {
                this._classEndDate = value;
            }
        }
        private string _primaryInstructor;
        [Column(Storage = "_primaryInstructor")]
        public string primaryInstructor
        {
            get
            {
                return this._primaryInstructor;
            }
            set
            {
                this._primaryInstructor = value;
            }
        }
        private string _secondaryInstructor;
        [Column(Storage = "_secondaryInstructor")]
        public string secondaryInstructor
        {
            get
            {
                return this._secondaryInstructor;
            }
            set
            {
                this._secondaryInstructor = value;
            }
        }
        private string _location;
        [Column(Storage = "_location")]
        public string location
        {
            get
            {
                return this._location;
            }
            set
            {
                this._location = value;
            }
        }
        private string _TDYCosts;
        [Column(Storage = "_TDYCosts")]
        public string TDYCosts
        {
            get
            {
                return this._TDYCosts;
            }
            set
            {
                this._TDYCosts = value;
            }
        }
        private string _studentCount;
        [Column(Storage = "_studentCount")]
        public string studentCount
        {
            get
            {
                return this._studentCount;
            }
            set
            {
                this._studentCount = value;
            }
        }
        private List<grades> _classGrades;
        [Column(Storage = "_classGrades")]
        public List<grades> classGrades
        {
            get
            {
                return this._classGrades;
            }
            set
            {
                this._classGrades = value;
            }
        }
AMCOMDB ADB = new AMCOMDB(connectionString);
        if (ADB.DatabaseExists())
        {

            var stud = ADB.GetTable<student>();
            var clas = ADB.GetTable<aClass>();
            IQueryable<string> query = from c in stud
                                       where c.studentName.Length > 5
                                       orderby c.studentName.Length
                                       select c.studentName.ToUpper();
            foreach (string name in query)
            {

            }
            //var q = from a in ADB.GetTable<student>()
            //       select a;
            //dtStudents = LinqQuerytoDataTable(q);
            //var q1 = from a in ADB.GetTable<aClass>()
            //         select a;
            //aClass c = new aClass();

            //dtClasses = reformatDataTable(q1);
        }

I receive the following when I try to get information from the database at (foreach (string name in query))

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Invalid object name 'student'.

I also get this when I first create the database:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.Linq.dll

Additional information: Unable to determine SQL type for 'System.Collections.Generic.List`1[WindowsFormsApplication1.Form1+grades]'.

remove the word Name that is what worked for me

[Table("student")]
public class student

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM