LINQ的问题:从数据库中读取对象,获取异常

时间:2013-01-09 12:46:15

标签: c# .net linq-to-sql

我对LINQ很新,我遇到了一些问题。我已经尝试谷歌搜索了一段时间,但我仍然没有找到任何问题的答案,所以我也会问。

我在Microsoft SQL Server上建立了一个测试数据库,其中包含两个表“Person2”和“Department2”。 Person2与Department2有多对一的关系。许多人只属于一个部门。

Person2具有以下属性:

  • id(int,Primary Key)
  • 姓名(nchar(50))
  • phoneNumber(nchar(10))
  • DepartmentID(int,外键,名称为:“fk_Department2_DepartmentID”)

Department2具有以下属性:

  • DepartmentID(int,Primary Key)
  • DepartmentDesc(nchar(50))

C#代码由两个项目文件中的四个类组成:一个是Person,Department和Program(运行测试),另一个是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);
                }
            }

        }
    }
}

问题的核心是Person类中的这段代码:

    [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列的数量不同   在.......等等等

我在搜索问题时发现的解决方案建议我将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操作的部门的Association-segment使用了两个键(只需打开ThisKey和OtherKey),并且可以工作。 换句话说:我在Person类中从数据库中获取Department的对象时遇到了麻烦,而它在Department代码中获取了Person对象的集合。

现在,亲爱的读者和程序员,您建议我做什么?

1 个答案:

答案 0 :(得分:1)

看起来它可能只是订单 - 你正在修改私人成员而不是公众;你试过这个吗?

private EntityRef<Department> _department;
[Association(Name = "FK_Person_PersonDepartment", IsForeignKey = true,
   Storage = "_department", ThisKey = "personDepartmentID", OtherKey = "departmentID")]
public Department Department
{
  get { return _department.Entity; }
  set { _department.Entity = value; }
}