简体   繁体   中英

How can I transfer the database logic from my Asp.Net MVC Apllication to ASP.Net Core MVC?

How can I transfer the database logic of this controller from Asp.Net MVC to ASP.Net Core MVC? I tried to apply several tutorials, but I couldn't transfer my logic with any of them. The application should register the user input and evaluate it in [HttpPost] and empty the database after 10 rounds.

That's what my Controller looks like:

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)
                {
                }
            }
        }
    }
}

That's my 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");
        }
    }
}

And here is my Database:

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; }
    }
}

And and my model:

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; }
    }
}

The error you are getting is that it can't convert a class of one type into another type. The error is being thrown on this line right?

List<MyApplicationData> _mad= dbContext.MyApplicationData.Select(a => a).ToList();

It's because you have two classes called MyApplicationData. One in the namespace of MyApplication.Models and one in the namespace of MyApplication. The application has two of them to choose from and it's grabbing the one you don't want. You have to force it to use the one you want by changing your line to this.

List<MyApplicationData.Models.MyApplicationData> _mad= dbContext.MyApplicationData.Select(a => a).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM