C# IEnumerable<;T>;返回复杂元素模具过程
我试图使用实体框架返回webapi中的元素列表 搜索工作正常。 数据格式正确,但是,在离开流程后,稍微考虑一下,应用程序就会死掉,显示错误:C# IEnumerable<;T>;返回复杂元素模具过程,c#,asp.net-mvc,entity-framework-4,C#,Asp.net Mvc,Entity Framework 4,我试图使用实体框架返回webapi中的元素列表 搜索工作正常。 数据格式正确,但是,在离开流程后,稍微考虑一下,应用程序就会死掉,显示错误: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll 源代码: [HttpGet] public IEnumerable<Teste> GetAllProducts() { using (var db = new
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
源代码:
[HttpGet]
public IEnumerable<Teste> GetAllProducts()
{
using (var db = new ERPContext())
{
db.Configuration.ProxyCreationEnabled = false;
var dados = (
from a in db.TipoPessoa
select new
{
a.TipoPessoaID,
a.Descricao,
a.UsuarioCad
}).AsQueryable().Select(item => new Teste { TipoPessoaID = item.TipoPessoaID, Descricao = item.Descricao, Usuario = item.UsuarioCad });
return dados.ToArray<Teste>();
}
}
其他类别:
public class TipoPessoa : IEntidadeBase
{
[Key]
public int TipoPessoaID { get; set; }
public string Sigla { get; set; }
public string Descricao { get; set; }
/* Campos fixos */
public int EmpresaID { get; set; }
public string Fixo { get; set; }
public string Status { get; set; }
public string Apagado { get; set; }
public DateTime? DtApagado { get; set; }
public int UsuCad { get; set; }
public DateTime DtCad { get; set; }
public int UsuAlt { get; set; }
public DateTime DtAlt { get; set; }
public int UsuUltAlt { get; set; }
public DateTime DtUltAlt { get; set; }
[ForeignKey("UsuCad")]
public virtual Usuario UsuarioCad { get; set; }
[ForeignKey("UsuAlt")]
public virtual Usuario UsuarioAlt { get; set; }
[ForeignKey("UsuUltAlt")]
public virtual Usuario UsuarioUltAlt { get; set; }
[ForeignKey("EmpresaID")]
public virtual Empresa Empresa { get; set; }
}
public class Usuario : IEntidadeBase
{
[Key]
public int UsuarioID { get; set; }
[Obrigatorio]
[Display(Name = "Login")]
public string Login { get; set; }
[Obrigatorio]
[Display(Name = "E-mail")]
public string Email { get; set; }
[Obrigatorio]
[Display(Name = "Nome")]
public string Nome { get; set; }
[Display(Name = "Senha")]
[StringLength(50)]
[DataType(DataType.Password)]
public string Senha { get; set; }
public string Chave { get; set; }
[Display(Name="Data de ativação do usuário")]
public Nullable<DateTime> DtAtivacao { get; set; }
[Display(Name = "Data da última conexão no sistema")]
public Nullable<DateTime> DtEntrada { get; set; }
[Display(Name = "Data da última saida do sistema")]
public Nullable<DateTime> DtSaida { get; set; }
public int? EmpresaIDLogada { get; set; }
public int? PessoaIDLogada { get; set; }
public int? ClienteID { get; set; }
public int? FilialID { get; set; }
[Display(Name = "Tipo de liberação")]
public string TipoLiberacao { get; set; }
[Display(Name = "Tipo de cadastro")]
public string TipoCadastro { get; set; }
public string AlterarSenha { get; set; }
public virtual string DtAtivacaoDesc
{
get
{
if (DtAtivacao != null)
{
return String.Format("{0:dd/MM/yyyy}", DtAtivacao);
}
else
{
return "ND";
}
}
}
[Display(Name = "Qtde. de acessos no sistema")]
public int QtdeAcesso { get; set; }
public int EmpresaID { get; set; }
public string Fixo { get; set; }
public string Status { get; set; }
public string Apagado { get; set; }
public DateTime? DtApagado { get; set; }
public int UsuCad { get; set; }
public DateTime DtCad { get; set; }
public int UsuAlt { get; set; }
public DateTime DtAlt { get; set; }
public int UsuUltAlt { get; set; }
public DateTime DtUltAlt { get; set; }
[ForeignKey("UsuCad")]
public virtual Usuario UsuarioCad { get; set; }
[ForeignKey("UsuAlt")]
public virtual Usuario UsuarioAlt { get; set; }
[ForeignKey("UsuUltAlt")]
public virtual Usuario UsuarioUltAlt { get; set; }
[ForeignKey("EmpresaID")]
public virtual Empresa Empresa { get; set; }
[ForeignKey("EmpresaIDLogada")]
public virtual Empresa EmpresaLogada { get; set; }
[ForeignKey("PessoaIDLogada")]
public virtual Pessoa PessoaLogada { get; set; }
[ForeignKey("ClienteID")]
public virtual Cliente Cliente { get; set; }
[ForeignKey("FilialID")]
public virtual Filial Filial { get; set; }
[ForeignKey("UsuarioID")]
public virtual ICollection<UsuarioAcesso> ListaAcessos { get; set; }
public virtual bool Criar()
{
return true;
}
}
public class TipoPessoa:IEntidadeBase
{
[关键]
public int TipoPessoaID{get;set;}
公共字符串Sigla{get;set;}
公共字符串描述符{get;set;}
/*Campos fixos*/
public int EmpresaID{get;set;}
公共字符串修复{get;set;}
公共字符串状态{get;set;}
公共字符串Apagado{get;set;}
公共日期时间?DtApagado{get;set;}
公共int UsuCad{get;set;}
公共日期时间DtCad{get;set;}
公共int UsuAlt{get;set;}
公共日期时间DtAlt{get;set;}
公共int UsuUltAlt{get;set;}
公共日期时间DtUltAlt{get;set;}
[外键(“UsuCad”)]
公共虚拟Usuario USUARICAD{get;set;}
[外键(“UsuAlt”)]
公共虚拟Usuario UsuarioAlt{get;set;}
[外键(“UsuUltAlt”)]
公共虚拟Usuario UsuarioUltAlt{get;set;}
[外国钥匙(“EmpresaID”)]
公共虚拟Empresa Empresa{get;set;}
}
公共类Usuario:IEntidadeBase
{
[关键]
public int UsuarioID{get;set;}
[obrigatio]
[显示(Name=“登录”)]
公共字符串登录{get;set;}
[obrigatio]
[显示(Name=“电子邮件”)]
公共字符串电子邮件{get;set;}
[obrigatio]
[显示(Name=“Nome”)]
公共字符串Nome{get;set;}
[显示(Name=“Senha”)]
[长度(50)]
[数据类型(数据类型.密码)]
公共字符串Senha{get;set;}
公共字符串Chave{get;set;}
[Display(Name=“Data de ativação do usuário”)]
公共可为空的dtativaco{get;set;}
[显示(Name=“Data daúltima conexão no sistema”)]
公共可为空的DtEntrada{get;set;}
[显示(Name=“Data daúltima saida do sistema”)]
公共可为空的DtSaida{get;set;}
public int?EmpresaIDLogada{get;set;}
公共int?PessoaIDLogada{get;set;}
public int?客户ID{get;set;}
公共int?FilialID{get;set;}
[显示(Name=“Tipo de liberação”)]
公共字符串tipoliberaco{get;set;}
[显示(Name=“Tipo de cadastro”)]
公共字符串tipocadstro{get;set;}
公共字符串{get;set;}
公共虚拟字符串DTA代码
{
得到
{
如果(dtativaco!=null)
{
返回String.Format(“{0:dd/MM/yyyy}”,dtativaco);
}
其他的
{
返回“ND”;
}
}
}
[显示(Name=“Qtde.de acessos无系统”)]
公共int QtdeAcesso{get;set;}
public int EmpresaID{get;set;}
公共字符串修复{get;set;}
公共字符串状态{get;set;}
公共字符串Apagado{get;set;}
公共日期时间?DtApagado{get;set;}
公共int UsuCad{get;set;}
公共日期时间DtCad{get;set;}
公共int UsuAlt{get;set;}
公共日期时间DtAlt{get;set;}
公共int UsuUltAlt{get;set;}
公共日期时间DtUltAlt{get;set;}
[外键(“UsuCad”)]
公共虚拟Usuario USUARICAD{get;set;}
[外键(“UsuAlt”)]
公共虚拟Usuario UsuarioAlt{get;set;}
[外键(“UsuUltAlt”)]
公共虚拟Usuario UsuarioUltAlt{get;set;}
[外国钥匙(“EmpresaID”)]
公共虚拟Empresa Empresa{get;set;}
[ForeignKey(“EmpresaIDLogada”)]
公共虚拟Empresa EmpresaLogada{get;set;}
[外汇(“PessoaIDLogada”)]
公共虚拟Pessoa PessoaLogada{get;set;}
[外国客户(“客户”)]
公共虚拟客户端客户端{get;set;}
[外国钥匙(“菲律宾”)]
公共虚拟机{get;set;}
[外键(“UsuarioID”)]
公共虚拟ICollection ListaAccessos{get;set;}
公共虚拟boolcriar()
{
返回true;
}
}
您离开流程后所说的是什么意思?我想问题出在你没有给我们看的代码里。你正在得到一个堆栈溢出。。。找出…堆栈跟踪将告诉您更多信息。在这段代码之后,返回dados.ToArray()代码>,进程IIS死亡
public class TipoPessoa : IEntidadeBase
{
[Key]
public int TipoPessoaID { get; set; }
public string Sigla { get; set; }
public string Descricao { get; set; }
/* Campos fixos */
public int EmpresaID { get; set; }
public string Fixo { get; set; }
public string Status { get; set; }
public string Apagado { get; set; }
public DateTime? DtApagado { get; set; }
public int UsuCad { get; set; }
public DateTime DtCad { get; set; }
public int UsuAlt { get; set; }
public DateTime DtAlt { get; set; }
public int UsuUltAlt { get; set; }
public DateTime DtUltAlt { get; set; }
[ForeignKey("UsuCad")]
public virtual Usuario UsuarioCad { get; set; }
[ForeignKey("UsuAlt")]
public virtual Usuario UsuarioAlt { get; set; }
[ForeignKey("UsuUltAlt")]
public virtual Usuario UsuarioUltAlt { get; set; }
[ForeignKey("EmpresaID")]
public virtual Empresa Empresa { get; set; }
}
public class Usuario : IEntidadeBase
{
[Key]
public int UsuarioID { get; set; }
[Obrigatorio]
[Display(Name = "Login")]
public string Login { get; set; }
[Obrigatorio]
[Display(Name = "E-mail")]
public string Email { get; set; }
[Obrigatorio]
[Display(Name = "Nome")]
public string Nome { get; set; }
[Display(Name = "Senha")]
[StringLength(50)]
[DataType(DataType.Password)]
public string Senha { get; set; }
public string Chave { get; set; }
[Display(Name="Data de ativação do usuário")]
public Nullable<DateTime> DtAtivacao { get; set; }
[Display(Name = "Data da última conexão no sistema")]
public Nullable<DateTime> DtEntrada { get; set; }
[Display(Name = "Data da última saida do sistema")]
public Nullable<DateTime> DtSaida { get; set; }
public int? EmpresaIDLogada { get; set; }
public int? PessoaIDLogada { get; set; }
public int? ClienteID { get; set; }
public int? FilialID { get; set; }
[Display(Name = "Tipo de liberação")]
public string TipoLiberacao { get; set; }
[Display(Name = "Tipo de cadastro")]
public string TipoCadastro { get; set; }
public string AlterarSenha { get; set; }
public virtual string DtAtivacaoDesc
{
get
{
if (DtAtivacao != null)
{
return String.Format("{0:dd/MM/yyyy}", DtAtivacao);
}
else
{
return "ND";
}
}
}
[Display(Name = "Qtde. de acessos no sistema")]
public int QtdeAcesso { get; set; }
public int EmpresaID { get; set; }
public string Fixo { get; set; }
public string Status { get; set; }
public string Apagado { get; set; }
public DateTime? DtApagado { get; set; }
public int UsuCad { get; set; }
public DateTime DtCad { get; set; }
public int UsuAlt { get; set; }
public DateTime DtAlt { get; set; }
public int UsuUltAlt { get; set; }
public DateTime DtUltAlt { get; set; }
[ForeignKey("UsuCad")]
public virtual Usuario UsuarioCad { get; set; }
[ForeignKey("UsuAlt")]
public virtual Usuario UsuarioAlt { get; set; }
[ForeignKey("UsuUltAlt")]
public virtual Usuario UsuarioUltAlt { get; set; }
[ForeignKey("EmpresaID")]
public virtual Empresa Empresa { get; set; }
[ForeignKey("EmpresaIDLogada")]
public virtual Empresa EmpresaLogada { get; set; }
[ForeignKey("PessoaIDLogada")]
public virtual Pessoa PessoaLogada { get; set; }
[ForeignKey("ClienteID")]
public virtual Cliente Cliente { get; set; }
[ForeignKey("FilialID")]
public virtual Filial Filial { get; set; }
[ForeignKey("UsuarioID")]
public virtual ICollection<UsuarioAcesso> ListaAcessos { get; set; }
public virtual bool Criar()
{
return true;
}
}