EF6 POCO 无法在 1..* 关系中转换 HashSet 类型的对象
EF6 POCO Unable to cast object of type HashSet in a 1..* Relationship
我正在尝试对具有以下结构的现有数据库使用 Entity Framework 6 和 POCO:
Departments
DepartmentID UNIQUEIDENTIFIER NOT NULL PK
SortOrder INT NULL
Image IMAGE NULL
Status BIT NULL
LastUpdated DATETIME NOT NULL
UpdatedBy NVARCHAR(10) NULL
Approved BIT NOT NULL
ApprovedBy NVARCHAR(10) NULL
ApprovedDate DATETIME NULL
ParentDepartment UNIQUEIDENTIFIER NULL
DepartmentDescriptions
DepartmentID UNIQUEIDENTIFIER NOT NULL PK, FK
LocaleID INT NOT NULL PK, FK
Description NVARCHAR(50) NOT NULL
Locales
LocaleID INT NOT NULL PK
ShortString NVARCHAR(10) NOT NULL
Description NVARCHAR(50) NOT NULL
Status BIT NOT NULL
我的 类 是:
public class Department
{
[Key]
public Guid DepartmentID { get; set; }
public Int32? SortOrder { get; set; }
public Byte[] Image { get; set; }
public Boolean Status { get; set; }
public DateTime LastUpdated { get; set; }
public String UpdatedBy { get; set; }
public Boolean Approved { get; set; }
public String ApprovedBy { get; set; }
public DateTime ApprovedDate { get; set; }
public Guid? ParentDepartment { get; set; }
// Navigation Properties
public virtual ICollection<DepartmentDescription> DepartmentDescriptions { get; set; }
public Department()
{
DepartmentDescriptions = new HashSet<DepartmentDescription>();
}
}
public class DepartmentDescription
{
[Key]
public Guid DepartmentID { get; set; }
public Int32 LocaleID { get; set; }
public String Description { get; set; }
// Navigation Properties
[ForeignKey("DepartmentID"), Required]
public virtual Department Department { get; set; }
[ForeignKey("LocaleID"), Required]
public virtual Locale Locale { get; set; }
}
public class Locale
{
[Key]
public Int32 LocaleID { get; set; }
public String ShortString { get; set; }
public String Description { get; set; }
public Boolean Status { get; set; }
// Navigation Properties
public virtual ICollection<DepartmentDescription> DepartmentDescriptions { get; set; }
public Locale()
{
DepartmentDescriptions = new HashSet<DepartmentDescription>();
}
}
当我尝试向上下文中添加新部门时,我得到:
*Unable to cast object of type 'System.Collections.Generic.HashSet`1[DepartmentDescription]' to type 'DepartmentDescription'.*
添加新部门的代码(转述)是:
Department _department = new Department
{
DepartmentID = new Guid("aed99956-c3e1-44a7-b09a-00169f64bdff"),
Status = true,
SortOrder = 320,
Image = null,
Approved = true,
ApprovedBy = "Import",
ApprovedDate = Convert.ToDateTime("11/22/2016 3:40:50PM"),
LastUpdated = Convert.ToDateTime("11/22/2016 3:40:50PM"),
UpdatedBy = Import
};
DepartmentDescription _description = new DepartmentDescription
{
DepartmentID = new Guid("aed99956-c3e1-44a7-b09a-00169f64bdff"),
LocaleID = 1033,
Description = "Department Description"
};
_department.DepartmentDescriptions.Add(_description);
context.Departments.Add(_department);
我确定我正在做一些值得打脸的事情,但我已经盯着它看太久了,看不出我错过了什么。任何建议将不胜感激!
DepartmentDescriptions table 有一个复合主键,DepartmentDescription class 没有表示。因此,DbContext 正在尝试使用未正确映射到数据库架构的 POCO 来执行其神奇的 ORM 操作。
使用 DataAnnotations,DepartmentDescription class 应该如下所示:
public class DepartmentDescription
{
[Key]
[Column(Order = 1)]
public Guid DepartmentID { get; set; }
[Key]
[Column(Order = 2)]
public Int32 LocaleID { get; set; }
public String Description { get; set; }
// Navigation Properties
[ForeignKey("DepartmentID"), Required]
public virtual Department Department { get; set; }
[ForeignKey("LocaleID"), Required]
public virtual Locale Locale { get; set; }
}
我正在尝试对具有以下结构的现有数据库使用 Entity Framework 6 和 POCO:
Departments
DepartmentID UNIQUEIDENTIFIER NOT NULL PK
SortOrder INT NULL
Image IMAGE NULL
Status BIT NULL
LastUpdated DATETIME NOT NULL
UpdatedBy NVARCHAR(10) NULL
Approved BIT NOT NULL
ApprovedBy NVARCHAR(10) NULL
ApprovedDate DATETIME NULL
ParentDepartment UNIQUEIDENTIFIER NULLDepartmentDescriptions
DepartmentID UNIQUEIDENTIFIER NOT NULL PK, FK
LocaleID INT NOT NULL PK, FK
Description NVARCHAR(50) NOT NULLLocales
LocaleID INT NOT NULL PK
ShortString NVARCHAR(10) NOT NULL
Description NVARCHAR(50) NOT NULL
Status BIT NOT NULL
我的 类 是:
public class Department
{
[Key]
public Guid DepartmentID { get; set; }
public Int32? SortOrder { get; set; }
public Byte[] Image { get; set; }
public Boolean Status { get; set; }
public DateTime LastUpdated { get; set; }
public String UpdatedBy { get; set; }
public Boolean Approved { get; set; }
public String ApprovedBy { get; set; }
public DateTime ApprovedDate { get; set; }
public Guid? ParentDepartment { get; set; }
// Navigation Properties
public virtual ICollection<DepartmentDescription> DepartmentDescriptions { get; set; }
public Department()
{
DepartmentDescriptions = new HashSet<DepartmentDescription>();
}
}
public class DepartmentDescription
{
[Key]
public Guid DepartmentID { get; set; }
public Int32 LocaleID { get; set; }
public String Description { get; set; }
// Navigation Properties
[ForeignKey("DepartmentID"), Required]
public virtual Department Department { get; set; }
[ForeignKey("LocaleID"), Required]
public virtual Locale Locale { get; set; }
}
public class Locale
{
[Key]
public Int32 LocaleID { get; set; }
public String ShortString { get; set; }
public String Description { get; set; }
public Boolean Status { get; set; }
// Navigation Properties
public virtual ICollection<DepartmentDescription> DepartmentDescriptions { get; set; }
public Locale()
{
DepartmentDescriptions = new HashSet<DepartmentDescription>();
}
}
当我尝试向上下文中添加新部门时,我得到:
*Unable to cast object of type 'System.Collections.Generic.HashSet`1[DepartmentDescription]' to type 'DepartmentDescription'.*
添加新部门的代码(转述)是:
Department _department = new Department
{
DepartmentID = new Guid("aed99956-c3e1-44a7-b09a-00169f64bdff"),
Status = true,
SortOrder = 320,
Image = null,
Approved = true,
ApprovedBy = "Import",
ApprovedDate = Convert.ToDateTime("11/22/2016 3:40:50PM"),
LastUpdated = Convert.ToDateTime("11/22/2016 3:40:50PM"),
UpdatedBy = Import
};
DepartmentDescription _description = new DepartmentDescription
{
DepartmentID = new Guid("aed99956-c3e1-44a7-b09a-00169f64bdff"),
LocaleID = 1033,
Description = "Department Description"
};
_department.DepartmentDescriptions.Add(_description);
context.Departments.Add(_department);
我确定我正在做一些值得打脸的事情,但我已经盯着它看太久了,看不出我错过了什么。任何建议将不胜感激!
DepartmentDescriptions table 有一个复合主键,DepartmentDescription class 没有表示。因此,DbContext 正在尝试使用未正确映射到数据库架构的 POCO 来执行其神奇的 ORM 操作。
使用 DataAnnotations,DepartmentDescription class 应该如下所示:
public class DepartmentDescription
{
[Key]
[Column(Order = 1)]
public Guid DepartmentID { get; set; }
[Key]
[Column(Order = 2)]
public Int32 LocaleID { get; set; }
public String Description { get; set; }
// Navigation Properties
[ForeignKey("DepartmentID"), Required]
public virtual Department Department { get; set; }
[ForeignKey("LocaleID"), Required]
public virtual Locale Locale { get; set; }
}