我是新的 ASP.NET MVC 5,但我已经在 Controller 中有工作代码来访问数据库,并且可以执行查询和存储过程。但在 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; }
}
}
然后我手动创建了一个 Controller :
namespace AsyncFileProcessor.Controllers
{
public class FileProcessStatusController : Controller
{
// GET: FileProcessStatus
public ActionResult Index()
{
return View();
}
}
}
对于 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 中轻松完成此操作:(
最佳答案
您可以使用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;
}
Dapper 轻量且速度非常快。如果我无法控制数据库或数据库未标准化,我会使用它。
关于c# - 如何在不使用 Entity Framework 的情况下使用 ASP NET MVC5 显示 SQL Server 中的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37869909/