C# LINQ to SQL 对象名称无效
Posted
技术标签:
【中文标题】C# LINQ to SQL 对象名称无效【英文标题】:C# LINQ to SQL Invalid Object Name 【发布时间】:2016-07-01 18:48:48 【问题描述】: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);
当我尝试在 (foreach (查询中的字符串名称)) 处从数据库中获取信息时,我收到以下信息
System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常
附加信息:无效的对象名称“学生”。
我在第一次创建数据库时也得到了这个:
System.Data.Linq.dll 中出现“System.InvalidOperationException”类型的未处理异常
附加信息:无法确定“System.Collections.Generic.List`1[WindowsFormsApplication1.Form1+grades]”的 SQL 类型。
【问题讨论】:
范围太广,无法回答。至少指出哪一行给你错误, foreach(查询中的字符串名称)行给出:System.Data.dll 中发生“System.Data.SqlClient.SqlException”类型的未处理异常附加信息:无效的对象名称“学生” '。 请将上述信息添加到问题中,以便读者不必通读 cmets 以获得足够的信息来提供帮助。谢谢! 要么没有连接到正确的数据库,要么没有足够的权限读取student
。
@Marrion 你能告诉我们 Cotntext 的 GetTable 方法吗?您是否尝试通过 var stud = ADB.allStudents 直接访问 allStudents 以隔离表 student 是否实际存在于您尝试连接的数据库中的问题?
【参考方案1】:
删除对我有用的名称一词
[Table("student")]
public class student
【讨论】:
以上是关于C# LINQ to SQL 对象名称无效的主要内容,如果未能解决你的问题,请参考以下文章