我对LINQ相当陌生,而且我遇到了一些问题。我尝试了一段时间的谷歌搜索,但我仍然没有找到任何精确的答案来解决我的问题,所以我也会问。LINQ问题:从数据库读取对象,出现异常
我已经在Microsoft SQL Server上建立了一个测试数据库,其中有两个表“Person2”和“Department2”。 Person2与Department2有多对一的关系。许多人只属于一个部门。
PERSON2具有以下属性:
- ID(INT,主键)
- 名称(NCHAR(50))
- phoneNumber的(NCHAR(10))
- DepartmentID的(INT,外国主要用名: “fk_Department2_DepartmentID”)
Department2具有以下属性:
- DepartmentID的(INT,主键)
- DepartmentDesc(NCHAR(50))
的C#-code由四个班的两个项目文件:人,部和方案(运行测试),另一个是TabellTest。 这是我试图运行代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace DBTest
{
[Database]
public class TableTest : DataContext
{
public Table<Person> persons;
public Table<Department> departments;
public TabellTest(String ConnectionString):
base(ConnectionString){}
}
}
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace DBTest
{
[Table(Name = "Person2")]
public class Person
{
public Person() { }
[Column(IsPrimaryKey = true)]
private int id;
public int Id
{
get { return this.id; }
set { this.id = value; }
}
[Column]
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
[Column]
private string phoneNumber;
public string PhoneNumber
{
get { return this.phoneNumber;}
set { this.phoneNumber = value;}
}
[Column (Name="DepartmentID")]
private int? personDepartmentID;
public int? PersonDepartmentID
{
get { return this.personDepartmentID; }
set { this.personDepartmentID = value; }
}
[Association(Name = "FK_Person_PersonDepartment",
IsForeignKey = true, Storage = "_department", ThisKey = "personDepartmentID")]
private EntityRef<Department> _department;
public Department Department
{
get { return _department.Entity; }
set { _department.Entity = value; }
}
}
[Table (Name = "Department2")]
public class Department {
public Department() { }
public Department(int departmentID, string departmentDesc)
{
this.departmentID = deparmentID;
this.departmentDesc = departmentDesc;
}
[Column(IsPrimaryKey = true)]
private int departmentID;
public int DepartmentID
{
get {return this.departmentID; }
set {this.departmentID = value; }
}
[Column]
private string departmentDesc;
public string DepartmentDesc
{
get { return this.departmentDesc; }
set { this.departmentDesc = value; }
}
private EntitySet<Person> _person = new EntitySet<Person>();
[Association(Name = "FK_Person_PersonDepartment",
IsForeignKey = true, Storage = "_person", ThisKey = "departmentID", OtherKey="personDepartmentID")]
public EntitySet<Person> person
{
get { return _person; }
set { _person = value; }
}
}
class Program
{
static void Main(string[] args)
{
TableTest Test = new TableTest("REMOVED FOR STACKOVERFLOW.COM, JUST ASSUME IT WORKS");
foreach (Person pers in Test.persons)
{
Console.WriteLine(pers.Name + " " + pers.Id + " " + pers.PhoneNumber + " " + pers.Department.DepartmentID + " " + pers.Department.DepartmentDesc);
}
Console.WriteLine("\n\n");
foreach (Department dep in Test.department)
{
Console.WriteLine(dep.DepartmentID + " " + dep.DepartmentDesc);
foreach (Person pers in dep.person)
{
Console.WriteLine(pers.Name + " " + pers.Id + " " + pers.PhoneNumber);
}
}
}
}
}
问题的核心是该代码段中的类人:
[Association(Name = "FK_Person_PersonDepartment", IsForeignKey = true,
Storage = "_department", ThisKey = "personDepartmentID")]
private EntityRef<Department> _department;
public Department Department
{
get { return _department.Entity; }
set { _department.Entity = value; }
}
每当我试着运行该程序,我得到此例外:
未处理的异常:System.InvalidOperationException:ThisKey列的数量与'Person'类型中关联属性'_department'的OtherKey列的数量不同 在.......等等等等等等
的溶液使用Google问题建议我的OtherKey属性添加到关联,在这种情况下,该协会代码变成这样,当我发现:
[Association(Name = "FK_Person_PersonDepartment", IsForeignKey = true,
Storage = "_department", ThisKey = "personDepartmentID", OtherKey = "departmentID")]
(我也试图与资本d:OtherKey = “DepartmentID的”)
当我这样做,我得到这个异常:
未处理的异常:System.InvalidOperationException:找不到类型为'EntityRef
1´. The key may be wrong or the field or property on ´EntityRef
1'的关键'departmentID'的关键成员'departmentID'已更改名称。 在....等等等等
具有讽刺意味的是,该协会段自处,这与EntitySet的操作同时使用的按键(只接通ThisKey和OtherKey),和作品。 换句话说:我无法从Person类的数据库中获取Department对象,但它可以在Department-Code中获取Person对象的集合。
现在,亲爱的读者和程序员,你建议我做什么?
(作者刘海自己的办公桌上头) 卫生署!... 它现在。非常感谢 :) –