以下是我要做的事情。我正在使用Entity Framework 6,我有一个DataLayer对象,我将这些层传递给BusinessData对象。基本上从数据库中获取对象并将其值传递给在BusinessData层中镜像它的新对象。
请参阅下面的代码。
实体框架生成的对象
public partial class SolutionArea
{
public SolutionArea()
{
this.Awards = new HashSet<Award>();
this.Competencies = new HashSet<Competency>();
this.KeyWins = new HashSet<KeyWin>();
this.Offerings = new HashSet<Offering>();
this.Products = new HashSet<Product>();
this.Programs = new HashSet<Program>();
}
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Manager { get; set; }
public string PreSalesCount { get; set; }
public virtual ICollection<Award> Awards { get; set; }
public virtual ICollection<Competency> Competencies { get; set; }
public virtual ICollection<KeyWin> KeyWins { get; set; }
public virtual ICollection<Offering> Offerings { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<Program> Programs { get; set; }
}
数据层对象
namespace SolutionsEntities.DataAccessObjects
{
public class SolutionAreaDAO
{
public SolutionAreaBDO GetSolutionArea(int Id)
{
SolutionAreaBDO solutionAreaBDO = null;
using(var context = new SolutionsEntities())
{
SolutionArea solutionAreaDAO = (from s in context.SolutionAreas
where s.ID == Id
select s).FirstOrDefault();
if (solutionAreaDAO != null)
{
solutionAreaBDO = new SolutionAreaBDO();
{
solutionAreaBDO.Title = solutionAreaDAO.Title;
solutionAreaBDO.Programs = solutionAreaDAO.Programs;
}
}
}
}
}
业务数据对象
namespace SolutionsBDO
{
public class SolutionAreaBDO
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Manager { get; set; }
public string PreSalesCount { get; set; }
public virtual ICollection<AwardBDO> Awards { get; set; }
public virtual ICollection<CompetencyBDO> Competencies { get; set; }
public virtual ICollection<KeyWinBDO> KeyWins { get; set; }
public virtual ICollection<OfferingBDO> Offerings { get; set; }
public virtual ICollection<ProductBDO> Products { get; set; }
public virtual ICollection<ProgramBDO> Programs { get; set; }
}
}
我遇到的问题是在上面的SolutionAreaDAO类中我得到一个错误,说我无法将ICollection转换为ICollection。这是导致问题的代码行:
solutionAreaBDO = new SolutionAreaBDO();
{
solutionAreaBDO.Title = solutionAreaDAO.Title;
solutionAreaBDO.Programs = solutionAreaDAO.Programs;
}
两个对象都具有属于单独实体的属性。 SolutionArea对象包含Program对象的Collection。当我尝试将集合从数据对象设置为业务对象时,我得到一个显式的强制转换错误。
Error 1 Cannot implicitly convert type 'System.Collections.Generic.ICollection<SolutionsEntities.Program>' to 'System.Collections.Generic.ICollection<SolutionsBDO.ProgramBDO>'. An explicit conversion exists (are you missing a cast?) C:\Projects\SolutionsBackgrounder\SolutionsEntities\DataAccessObjects\SolutionAreaDAO.cs 26 52 SolutionsEntities
我确定我没有做对,但如果有人能指出我正确的方向,我将不胜感激!
谢谢,
乔
答案 0 :(得分:2)
您正在尝试将ICollection<SolutionsEntities.Program>>
转换为ICollection<SolutionsBDO.ProgramBDO>
这是两个具有不同类型参数的集合(就编译器而言,它们是无关的)。您需要手动转换这些对象,更改BDO或DAO上的集合类型,或使用AutoMapper之类的东西自动转换它们