简体   繁体   中英

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

I'm kind of new ASP.NET MVC 5, but I already have working code in a controller to access database and can perform queries and stored procedure. But after searching Google for answers, I'm still lost on how to be able to retrieve data from my database and display it in a table format without using Entity Framework due to reasons from manager.

In simple words, I just want to do something as simple as select * from table and be able to display the data in a table format.

Here's what I have tried:

I tried to create a model class via ADO.NET and got the following 2 classes:

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; }
    }
}

Then I manually created a controller:

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

For view, I just want the table to be displayed in 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>

I tried @model IEnumerable<AsyncFile.....> in Index.cshtml , but it doesn't recognize anything related to my model.

Tried a lot of solutions online, but my lack of knowledge about MVC5 fails me.

If someone can help me figure out how I can accomplish this and maybe explain the whole MVC5 workflow in simple terms, I would appreciate it.

I can do this easily in Django :(

You can use Dapper ORM developed by StackExchange .

It basically maps an object-oriented domain model to a traditional relational database. Otherwise, you will have to use ADO.Net .

For example,

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 is lightweight and very fast. I use it, if I do not have control over Database or Database is not normalized.

If you want to write less SQL, but still have Dapper-like performance, you can use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM