实体框架4.1 - 如何使DbContext实体可序列化

时间:2011-05-24 12:46:58

标签: .net entity-framework workflow-foundation entity-framework-4.1

我正在使用EF4.1 DbContext代码生成,它会创建像这样的POCO实体:

public partial class Employee
{
    public Employee()
    {
        this.Roles = new HashSet<Role>();
    }

    public System.Guid EmployeeGUID { get; set; }
    public string EmployeeID { get; set; }
    public string PIN { get; set; }
    public string FullName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string JobTitle { get; set; }
    public string DepartmentDescription { get; set; }
    public Nullable<System.DateTime> LatestHireDate { get; set; }
    public string CompanyEmailAddress { get; set; }
    public Nullable<System.Guid> SupervisorGUID { get; set; }
    public string SupervisorFullName { get; set; }
    public string SupervisorCompanyEmailAddress { get; set; }
    public string JobCode { get; set; }

    public virtual ICollection<Role> Roles { get; set; }
}

这是如何完成的?

1 个答案:

答案 0 :(得分:5)

我会创建代表真正需要序列化的DTO(数据传输对象),而不是序列化您的DBContext实体。然后,我会使用类似AutoMapper的东西将您的DBContext实体映射到您的DTO。

更新:This is a bit out of date,但作者解释了AutoMapper的一些重要用途。

相关问题