I am trying to use Entity Framework 6 and POCOs against an existing database with the following structure:
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
My classes are:
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>();
}
}
When I try to add a new Department to the context I get:
*Unable to cast object of type 'System.Collections.Generic.HashSet`1[DepartmentDescription]' to type 'DepartmentDescription'.*
The code to add a new department (paraphrased) is:
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);
I'm sure I'm doing something that will deserve a facepalm, but I've been staring at this too long to see what I'm missing. Any suggestions would be greatly appreciated!
Department. - Matt Rowland_department.DepartmentDescriptions.Add(_description);,_descriptionis a HashSet. - Gert Arnold