简体   繁体   English

EF6 POCO无法在1 .. *关系中强制转换HashSet类型的对象

[英]EF6 POCO Unable to cast object of type HashSet in a 1..* Relationship

I am trying to use Entity Framework 6 and POCOs against an existing database with the following structure: 我试图针对具有以下结构的现有数据库使用Entity Framework 6和POCO:

Departments 部门
DepartmentID UNIQUEIDENTIFIER NOT NULL PK DepartmentID UNIQUEIDENTIFIER NOT NULL PK
SortOrder INT NULL SortOrder INT NULL
Image IMAGE NULL 图片IMAGE NULL
Status BIT NULL 状态位为空
LastUpdated DATETIME NOT NULL LastUpdated DATETIME NOT NULL
UpdatedBy NVARCHAR(10) NULL 由NVARCHAR(10)NULL更新
Approved BIT NOT NULL 批准的BIT不为空
ApprovedBy NVARCHAR(10) NULL 已通过NVARCHAR(10)批准
ApprovedDate DATETIME NULL ApprovedDate DATETIME NULL
ParentDepartment UNIQUEIDENTIFIER NULL ParentDepartment UNIQUEIDENTIFIER NULL

DepartmentDescriptions DepartmentDescriptions
DepartmentID UNIQUEIDENTIFIER NOT NULL PK, FK DepartmentID UNIQUEIDENTIFIER NOT NULL PK,FK
LocaleID INT NOT NULL PK, FK LocaleID INT非空PK,FK
Description NVARCHAR(50) NOT NULL 说明NVARCHAR(50)NOT NULL

Locales 语言环境
LocaleID INT NOT NULL PK LocaleID INT非空PK
ShortString NVARCHAR(10) NOT NULL ShortString NVARCHAR(10)非空
Description NVARCHAR(50) NOT NULL 说明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! 任何建议将不胜感激!

The DepartmentDescriptions table has a composite primary key which isn't represented by the DepartmentDescription class. DepartmentDescriptions表具有一个复合主键,该主键未由DepartmentDescription类表示。 As a result, the DbContext is trying to do its magical ORM stuff with POCOs that don't map to the database schema properly. 结果,DbContext尝试使用POCO(无法正确映射到数据库模式)来完成其神奇的ORM。

Using DataAnnotations, the DepartmentDescription class should look like this: 使用DataAnnotations,DepartmentDescription类应如下所示:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM