【问题标题】:how to resolve web api error 'System.OutOfMemoryException'如何解决 Web api 错误“System.OutOfMemoryException”
【发布时间】:2020-03-05 05:23:59
【问题描述】:

我正在创建 ASP.Net MVC WebApi 以将我的数据共享到银行。在这方面,我在测试我的 WebApi 时创建了一个 SQL 视图 位,它给出了一个 'System.OutOfMemoryException' 错误,因为我在 SQL 中有更多的 100 万条记录看法。 我的代码如下: - 这是我的控制器

 public class InvoiceController : ApiController
    {
        public IEnumerable<VBank_invoice> Get()
        {
            using (kmcEntities entities = new kmcEntities())
            {
                return entities.VBank_invoice.ToList();
            }
        }

        public VBank_invoice Get(string consumer)
        {
            using (kmcEntities entities = new kmcEntities())
            {
                return entities.VBank_invoice.FirstOrDefault(e => e.consumer_no == consumer);
            }
        }
    }

My SQL 视图类

public partial class VBank_invoice
    {
        public int sno { get; set; }
        public string consumer_no { get; set; }
        public string consumer_name { get; set; }
        public string consumer_address { get; set; }
        public string billing_month { get; set; }
        public Nullable<decimal> current_Charges { get; set; }
        public Nullable<decimal> outstanding_Arrears { get; set; }
        public Nullable<decimal> Arrears_15 { get; set; }
        public Nullable<decimal> part_payment_arrears { get; set; }
        public string billing_period_code { get; set; }
        public string consumer_checkdigit { get; set; }
        public Nullable<System.DateTime> due_date { get; set; }
    }

这是我的 Model.Context.cs 文件

 public partial class kmcEntities : DbContext
    {
        public kmcEntities()
            : base("name=kmcEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<VBank_invoice> VBank_invoice { get; set; }
    }

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-web-api asp.net-web-api2 sql-view


    【解决方案1】:

    我相信您需要使用选择查询来序列化对象模型。

    public class InvoiceController : ApiController
    {
        public IEnumerable<VBank_invoice> Get()
        {
            using (kmcEntities entities = new kmcEntities())
            {
                return entities.VBank_invoice.select(m => new {
               m.sno, m.consumer_no,m.consumer_name,  m.consumer_address, 
               m.billing_month, m.current_Charges, m.outstanding_Arrears, 
               m.Arrears_15, m.part_payment_arrears, m.billing_period_code, 
               m.consumer_checkdigit, m.due_date }).ToList();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 2022-08-19
      • 2021-05-18
      • 2019-02-10
      • 2019-12-06
      相关资源
      最近更新 更多