Entity Framework 4.1 - 如何使 DbContext 实体可序列化
我正在使用 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; }
}
这是如何完成的?
I'm using the EF4.1 DbContext code generation, which creates POCO entities like this:
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; }
}
How is this accomplished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会序列化 DBContext 实体,而是创建代表真正需要序列化的内容的 DTO(数据传输对象)。然后,我将使用 AutoMapper 之类的东西将 DBContext 实体映射到 DTO。
更新:这有点过时了,但作者解释了一些AutoMapper 的巨大用途。
Instead of serializing your DBContext entities, I would create DTOs (Data Transfer Object) that represents what really needs to be serialized. Then, I would use something like AutoMapper to map your DBContext entities to your DTOs.
Update: This is a bit out of date, but the author explains some of the great uses for AutoMapper.