Как я могу перенести логи базы данных c этого контроллера из Asp. Net MVC в ASP. Net Core MVC? Я попытался применить несколько учебных пособий, но не смог перенести свои логи c ни с одной из них. Приложение должно зарегистрировать пользовательский ввод и оценить его в [HttpPost] и очистить базу данных после 10 раундов.
Вот так выглядит мой контроллер:
using Microsoft.AspNetCore.Mvc;
using MyApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.AspNetCore;
namespace MyApplication.Controllers
{
public class MyController : Controller
{
public IActionResult MyApplicationView()
{
ClearDataBase();
Model model = new Model();
int index = 0;
model.Image= GetImage(out index);
model.Round = 1;
model.Direction = 0;
model.Fast = 0;
model.Slow = 0;
model.Good = 0;
return View("MyApplicationView", model);
}
private string GetImage(out int index)
{
Random random = new Random();
var list = new List<string> { "0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "110", "120", "130", "140", "150", "160", "170", "180", "190", "200" };
index = random.Next(1, list.Count);
string image = "~/Images/" + list[index] + ".jpg";
int intindex = Convert.ToInt32(list[index]);
using (DatabaseContext dbContext = new DatabaseContext())
{
MyApplicationData _mad = null;
bool newDBtable = false;
try
{
myApplicationData = dbContext.MyApplicationData.FirstOrDefault();
}
catch (Exception ex)
{
}
if (_mad == null)
{
newDBtable = true;
_mad = new MyApplicationData(1);
_mad.Rounds = 1;
_mad.TooSlow = 0;
_mad.TooFast = 0;
_mad.GoodDrive = 0;
}
else
{
newDBtable = false;
}
_agd.ImageIndex = intindex;
try
{
if (newDBtable = true)
{
dbContext.MyApplicationData.Add(_mad);
}
else
{
dbKontext.MyApplicationData.Attach(_mad);
dbKontext.Entry(_mad).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
int numberOfChanges = dbContext.SaveChanges();
}
catch (Exception ex)
{
}
}
return image;
}
[HttpPost]
public IActionResult TimerCompleted(Model model)
{
MyApplicationData _mad = null;
bool newDBtable = false;
using (DatabaseContext dbKontext = new DatabaseContext())
{
try
{
myApplicationData = dbContext.MyApplicationData.FirstOrDefault();
}
catch (Exception ex)
{
}
if (_mad == null)
{
_mad = new MyApplicationData(1);
_mad.Rounds = 1;
newDBtable = true;
_mad.TooSlow = 0;
_mad.TooFast = 0;
_mad.GoodDrive = 0;
}
if (_mad.ImageIndex < model.Direction)
{
model.Slow = _mad.TooSlow;
model.Fast = ++_mad.TooFast;
model.Good = _mad.GoodDrive;
}
else if (_mad.ImageIndex > model.Direction)
{
model.Slow = ++_mad.TooSlow;
model.Fast = _mad.TooFast;
model.Good = _mad.GoodDrive;
}
else if (_mad.ImageIndex == model.Direction)
{
model.Slow = _mad.TooSlow;
model.Fast = _mad.TooFast;
model.Good = ++_mad.GoodDrive;
}
else
{
model.Slow = _mad.TooSlow;
model.Fast = ++_mad.TooFast;
model.Good = _mad.GoodDrive;
}
if (_mad.Rounds <= 10)
{
_mad.Rounds = _mad.Rounds + 1;
model.Round = _mad.Rounds ;
}
else
{
model.Round = _mad.Rounds ;
}
try
{
if (newDBtable == true)
{
dbContext.MyApplicationData .Add(_mad);
}
else
{
dbKontext.MyApplicationData.Attach(_mad);
dbKontext.Entry(_mad).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
int numberOfChanges = dbContext.SaveChanges();
}
catch (Exception ex)
{
}
}
int index;
model.Image = Url.Content(GetImage(out index));
return Json(model);
}
public void EmpyDatabase()
{
using (DatabaseContext dbContext = new DatabaseContext ())
{
try
{
List<MyApplicationData> _mad= dbContext.MyApplicationData.Select(a => a).ToList();
foreach (MyApplicationData mad in _mad)
{
dbContext.AutoGaugeDaten.Attach(mad);
dbContext.Entry(mad).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
}
int numberOfChanges = dbContext.SaveChanges();
}
catch (Exception ex)
{
}
}
}
}
}
Это мой DbContext
using Microsoft.EntityFrameworkCore;
namespace MyApplication.Models
{
public class DatabaseContext : DbContext
{
public virtual DbSet<MyApplicationData> MyApplicationData { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=MyApplicationDatabaseCore;Integrated Security=True");
}
}
}
А вот и моя база данных:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MyApplication.Models
{
[Table("MyApplicationData")]
public partial class MyApplicationData
{
protected MyApplicationData()
{
}
public MyApplicationData(int MyApplicationDataID)
{
this.MyApplicationDataID= MyApplicationDataID;
}
[Key]
public int MyApplicationDataID{ get; set; }
public int Rounds{ get; set; }
public int TooFast{ get; set; }
public int TooSlow{ get; set; }
public int ImageIndex { get; set; }
public int GoodDrive{ get; set; }
}
}
А и моя модель:
namespace MyApplication.Models
{
public class GaugeModel
{
public string Image{ get; set; }
public int Fast{ get; set; }
public int Slow{ get; set; }
public int Direction{ get; set; }
public int Round{ get; set; }
public int Good{ get; set; }
}
}