2
votes

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     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

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!

1
You're trying to add a HashSet to a HashSet. - Gert Arnold
To show what @GertArnold is talking about, can you add the code in question. Specifically the part where you are trying to add a new Department. - Matt Rowland
Added the code to add a new Department - CihSoft
paraphrased -- The error suggests that in reality, in _department.DepartmentDescriptions.Add(_description);, _description is a HashSet. - Gert Arnold
Agreed. That's where I'm confused. DepartmentDescriptions is a HashSet of DepartmentDescription objects but _description is a single DepartmentDescription. The way I'm interpreting the error, EF is expecting a single description and I'm passing it a HashSet (or a List because I've tried that too) :-) - CihSoft

1 Answers

0
votes

The DepartmentDescriptions table has a composite primary key which isn't represented by the DepartmentDescription class. As a result, the DbContext is trying to do its magical ORM stuff with POCOs that don't map to the database schema properly.

Using DataAnnotations, the DepartmentDescription class should look like this:

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; }

}