我不明白错误..我认为据我所知有一个dataType,但我怎么看?

时间:2018-05-02 11:31:54

标签: c# sql-server linq-to-sql mapping

enter image description here

在下面的代码中,我希望我正确指出什么是错误的。我需要能够调用item.departments.dept_Type,这应该是可能的,因为我已经建立了关联。如果我理解正确,我不需要在查询上创建内部联接以获取数据。 这是我的PersonClass

    namespace DATALAYER.DataHandler
{

    [Table(Name = "People")]

        public class Person 
        {


        private int _DepartmentID;
        public EntityRef<Department> _Department;
        public Person() { this._Department = new EntityRef<Department>(); }
        private int _ID;
            [Column(IsPrimaryKey =true, Storage ="_ID")]
            public int ID
            {
                get { return this._ID; }
                set { this._ID = value; }
            }
            private string _p_FirstName;
            [Column(Storage = "_p_FirstName")]
            public string p_FirstName
            {
                get { return this._p_FirstName; }
                set { this._p_FirstName = value; }
            }
            private string _LastName;
            [Column(Storage = "_LastName")]
            public string p_LastName
            {
                get { return this._LastName; }
                set { this._LastName = value; }
            }
            private string _EmailAddress;
            [Column(Storage = "_EmailAddress")]
            public string p_EmailAddress
            {
                get { return this._EmailAddress; }
                set { this._EmailAddress = value; }
            }
            private string _Password;
            [Column(Storage = "_Password")]
            public string p_Password
            {
                get { return this._Password; }
                set { this._Password = value; }
            }
            private string _SSID;
            [Column(Storage = "_SSID")]
            public string p_SSID
            {
                get { return this._SSID; }
                set { this._SSID = value; }
            }
            private string _DOB;
            [Column(Storage = "_DOB")]
            public string p_DOB
            {
                get { return this._DOB; }
                set { this._DOB = value; }
            }
            private string _CellNumber;
            [Column(Storage = "_CellNumber")]
            public string p_CellNumber
            {
                get { return this._CellNumber; }
                set { this._CellNumber = value; }
            }

            [Column(Storage = "_DepartmentID", DbType = "Int")]
            public int p_Department_dept_ID
            {
                get { return this._DepartmentID; }
                set { this._DepartmentID = value; }
            }

            [Association(Storage = "_DepartmentID", ThisKey = "p_Department_dept_ID")]
            public Department Department
            {
                get { return this._Department.Entity; }
                set { this._Department.Entity = value; }
            }
    }

}

这是我的部门代码

    namespace DATALAYER.DataHandler
{
    [Table(Name = "Departments")]
    public class Department
    {
        //private EntitySet<Person> _Person;
        //public Department()
        //{
        //    this._Person = new EntitySet<Person>();
        //}
        private int _DepartmentID;
        [Column(IsPrimaryKey = true, Storage = "_DepartmentID")]
        public int dept_ID
        {
            get { return this._DepartmentID; }
            set { this._DepartmentID = value; }

        }
        private string _deptType;
        [Column(Storage = "_deptType")]
        public string dept_Type
        {
            get { return this._deptType; }
            set { this._deptType = value; }
        }
        //[Association(Storage = "_Person", OtherKey = "ID")]
        //public EntitySet<Person> Persons
        //{
        //    get { return this._Person; }
        //    set { this._Person.Assign(value); }
        //}

    }
}

现在我想到的问题是,人员的主键和部门的外键之间存在数据类型问题。但由于它们都是int,我不明白这是怎么回事。

如果有人可以解释我的问题,如果我错了或者某事或者帮我解决问题,请解决。

添加了这个课程

namespace DATALAYER.DataHandler
{
public class SHSdb2 : DataContext
{
    public Table<Person> People;
    public Table<Department> Department;
    //public Table<Address> Address;
    public SHSdb2(string connection) : base(connection) { }
}

} 人员表 Person table 部门表 Department table

没有这个代码它可以工作但我只能调用表中的东西

        [Column(Storage = "_DepartmentID", DbType = "Int")]
    public int p_Department_dept_ID
    {
        get { return this._DepartmentID; }
        set { this._DepartmentID = value; }
    }

    [Association(Storage = "_DepartmentID", ThisKey = "p_Department_dept_ID")]
    public Department Department
    {
        get { return this._Department.Entity; }
        set { this._Department.Entity = value; }
    }

1 个答案:

答案 0 :(得分:0)

似乎我修好了这个不确定是否有修复,或者我很幸运,如果有人仍然可以向我解释这将非常感激。

var personQuery =
            from per in db.People
            where per.p_FirstName == "Christian" 
            select per.Department;

我在想通过指定它需要从部门获取数据,它将允许它在foreach中。