2016-06-16 33 views
2

我是一种新的ASP.NET MVC 5,但我已经有一个控制器中的工作代码来访问数据库,并可以执行查询和存储过程。但在搜索Google的答案后,我仍然因为经理的原因而无法使用Entity Framework从数据库中检索数据并以表格格式显示。如何在不使用Entity Framework的情况下使用ASP NET MVC5从SQL Server显示表?

简而言之,我只想做一些简单的事情,如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> 

我试过@model IEnumerable<AsyncFile.....>Index.cshtml,但它不能识别任何与我的模型相关的东西。

在网上尝试了很多解决方案,但是我对MVC5的知识缺乏让我失望。

如果有人可以帮我弄清楚我可以如何完成这个工作,也许可以用简单的术语解释整个MVC5工作流程,我将不胜感激。

我可以在Django很容易做到这一点:(

回答

3

您可以使用Dapper 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; 
} 

小巧玲珑,速度非常快。我使用它,如果我没有控制数据库或数据库未规范化。

+0

哇不知道ORM,看起来很酷。 但是,我仍然不知道如何将这些碎片连接在一起。 一旦我获得从我的查询返回的强类型列表,如何使用它来创建我想在index.cshtml中查看的视图?如果这是一个愚蠢的问题,并且已经得到解答,请在那指向我。谢谢! – ygongdev

+1

如果您是ASP.Net MVC的新手,您可能需要阅读[Adam Freeman编写的Pro ASP.NET MVC 5](https://www.amazon.com/Pro-ASP-NET-Experts-Voice-ASP- Net/dp/1430265299/ref = sr_1_2?ie = UTF8&qid = 1466138431&sr = 8-2&keywords = asp.net + mvc)或观看免费课程[Scott MVC 5基础知识](https://www.pluralsight。COM /课程/ aspdotnet-mvc5基本面)。 – Win

+0

我发现这个链接对我很有帮助。通过查看工作代码的例子,我学到了很多东西。但是,感谢您向我介绍Dapper:D [link](http://www.c-sharpcorner.com/UploadFile/0c1bb2/crud-operations-in-Asp-Net-mvc-5-using-dapper-orm/ ) – ygongdev

0

如果你想编写更少的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

相关问题