C# 带有NHibernate响应的Web API自定义序列化
我使用webapi和NHibernateORM创建应用程序。调用get方法时出现问题。NHibernate Fluent映射中有许多关系。例如:C# 带有NHibernate响应的Web API自定义序列化,c#,serialization,nhibernate,fluent-nhibernate,asp.net-web-api2,C#,Serialization,Nhibernate,Fluent Nhibernate,Asp.net Web Api2,我使用webapi和NHibernateORM创建应用程序。调用get方法时出现问题。NHibernate Fluent映射中有许多关系。例如: public class Case : GuidEntityBase { public virtual CaseType CaseType { get; set; } public virtual string CaseNumber { get; set; } public virtual DateTime CaseDate {
public class Case : GuidEntityBase
{
public virtual CaseType CaseType { get; set; }
public virtual string CaseNumber { get; set; }
public virtual DateTime CaseDate { get; set; }
public virtual IList<Document> Documents { get; set; }
public Case()
{
Documents = new List<Document>();
}
}
public class Document : GuidEntityBase
{
public virtual DocumentType DocumentType { get; set; }
public virtual string DocumentNumber { get; set; }
public virtual DateTime DocumentDate { get; set; }
public virtual Case Case { get; set; }
}
我得到了文档数据,但同时我也得到了案例数据。如何筛选此过程?我写了response.Value.Case=null,但这不是解决问题的好方法。跨视图发送实体是个坏主意,您应该做的是基于视图创建模型,填充模型并跨视图发送
public class DocumentDto
{
public Guid Id { get; set; }
public DocumentType DocumentType { get; set; }
public string DocumentNumber { get; set; }
public DateTime DocumentDate { get; set; }
}
[Route("api/document/GetItem/{id}")]
[HttpGet]
public DocumentDto GetItem(string Id)
{
var doc = service.GetItem(Id).Value;
return new DocumentDto(){
Id = doc.Id,
//set other properties from doc
};
}
跨视图发送实体是个坏主意,您应该做的是基于视图创建一个模型,填充它并跨视图发送
public class DocumentDto
{
public Guid Id { get; set; }
public DocumentType DocumentType { get; set; }
public string DocumentNumber { get; set; }
public DateTime DocumentDate { get; set; }
}
[Route("api/document/GetItem/{id}")]
[HttpGet]
public DocumentDto GetItem(string Id)
{
var doc = service.GetItem(Id).Value;
return new DocumentDto(){
Id = doc.Id,
//set other properties from doc
};
}
为什么不关闭延迟加载?我不是NHibernate用户,但我确信,有一个选项可以做到这一点。为什么不关闭延迟加载?我不是NHibernate用户,但我确信,有一个选项可以做到这一点。