【发布时间】:2018-02-05 23:20:32
【问题描述】:
我正在使用 ASP.NET Core 2,并且我有一个运行 Tasks 的控制器。
例如,下面是一个简单的文件上传,我有其他运行其他 Tasks 或 Actions 并且我希望能够将事件或任务记录到我的数据库中。
所以我需要创建一个类来在数据库上运行INSERTs 或UPDATEs。
如何创建一个使用DbContext 或使用类(而不是控制器)调用存储过程的类来执行我的数据库操作?
这是我的控制器代码之一的示例:
public async Task<IActionResult> Uploads(string fullName, IFormFile pic)
{
try {
string user = null;
try
{
user = User.Identity.Name.ToString();
}
catch (Exception err)
{
user = "anonymous";
}
if (user == null)
{
user = "";
}
string path = he.WebRootPath + "/uploads/" + user ;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
DateTime date = DateTime.Now ;
var dates = date.ToLongDateString();
var ext = Path.GetExtension(path).ToLowerInvariant();
var fileName = Path.Combine(he.WebRootPath + "/uploads/" + "/" + user, Path.GetFileName(pic.FileName));
var f = Path.Combine(he.WebRootPath + "/uploads/" + "/" + user, dates+Path.GetFileName(pic.FileName));
int i = 0;
ViewData["fname"] = fullName;
if (pic != null || pic.Length == 0)
{
using (var stream = new FileStream(fileName, FileMode.Create))
{
await pic.CopyToAsync(stream);
}
ViewData["fileLocation"] = "/uploads/" + user + "/" + Path.GetFileName(pic.FileName);
// }
}
【问题讨论】:
标签: asp.net-mvc asp.net-core dbcontext