[英]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.