簡體   English   中英

如何在不使用實體框架的情況下使用ASP NET MVC5從SQL Server顯示表?

[英]How to display table from SQL Server using ASP NET MVC5 without using Entity Framework?

我是新的ASP.NET MVC 5,但我已經在控制器中具有工作代碼以訪問數據庫,並且可以執行查詢和存儲過程。 但是在Google搜索答案之后,由於管理器的原因,我仍然不知道如何能夠從數據庫中檢索數據並以表格格式顯示數據而不使用Entity Framework。

用簡單的話來說,我只想做一些簡單的事情,例如select * from table並且能夠以表格式顯示數據。

這是我嘗試過的:

我嘗試通過ADO.NET創建模型類,並獲得了以下2個類:

namespace AsyncFileProcessor.Models
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

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

        public virtual DbSet<AsyncFileProcessingQueue> AsyncFileProcessingQueues { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
}

namespace AsyncFileProcessor.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("AsyncFileProcessingQueue")]
    public partial class AsyncFileProcessingQueue
    {
        public int ID { get; set; }

        [Required]
        [StringLength(256)]
        public string Filename { get; set; }

        [StringLength(256)]
        public string Path { get; set; }

        public int Status { get; set; }

        public DateTime CreatedDate { get; set; }

        public int CreatedById { get; set; }

        public DateTime UpdatedDate { get; set; }

        public int UpdatedById { get; set; }
    }
}

然后,我手動創建了一個控制器:

namespace AsyncFileProcessor.Controllers
{
    public class FileProcessStatusController : Controller
    {
        // GET: FileProcessStatus
        public ActionResult Index()
        {
            return View();
        }
    }
} 

為了查看,我只希望表顯示在Index.cshtml中

@{
    ViewBag.Title = "DynamicFileUploader";
}
              //Stuff here....

                <div class="col-lg-6">
                    <h1 style="text-align:center;">Placeholder</h1>
                    // The table would go inside this section
                </div>

我在Index.cshtml嘗試了@model IEnumerable<AsyncFile.....> ,但它無法識別與我的模型相關的任何內容。

在線嘗試了很多解決方案,但是由於我對MVC5的了解不足,使我感到失望。

如果有人可以幫助我弄清楚如何實現這一目標,或者可以用簡單的方式解釋整個MVC5工作流程,我將不勝感激。

我可以在Django中輕松做到這一點:(

你可以用小巧玲瓏的ORM通過StackExchange開發。

它基本上將面向對象的域模型映射到傳統的關系數據庫。 否則,您將不得不使用ADO.Net

例如,

public async Task<IList<AsyncFileProcessingQueue>> GetAsyncFileProcessingQueue()
{
   IList<AsyncFileProcessingQueue> result;
   using (IDbConnection conn = new SqlConnection(DataConnectionString))
   {
      conn.Open();

      var entities = await conn.QueryAsync<AsyncFileProcessingQueue>
          ("SELECT * FROM YOUR_TABLE");

      result = entities.ToList();
   }
   return result;
}

Dapper很輕巧而且非常快。 如果我對數據庫沒有控制權或數據庫未規范化,我將使用它。

如果您想編寫更少的SQL,但仍具有類似Dapper的性能,則可以使用Tortuga Chain。

using Tortuga.Chain;

static SqlServerDataSource s_Data = new SqlServerDataSource(DataConnectionString);

public async Task<IList<AsyncFileProcessingQueue>> GetAsyncFileProcessingQueue()
{
   return s_Data.From("YOUR_TABLE").ToCollection<AsyncFileProcessingQueue>().ExecuteAsync();
}

https://github.com/docevaad/Chain/wiki/A-Chain-comparison-to-Dapper

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM