E
E
evgemiil2017-06-14 00:19:26
ASP.NET
evgemiil, 2017-06-14 00:19:26

How to run the first class correctly?

Here is the MVC config

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using WebApplication.Models;

namespace WebApplication
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Database.SetInitializer(new StaticDbInitializer());

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

When run, the class should fire first:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.IO;
using XO;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication.Models
{
    public class StaticDbInitializer
    {
        public void StatisticDB()
        {
            string jsonData = @"C:\Users\EvgenieL\Source\Repos\XO\XO\XO\bin\Debug\stats.json";

            List<Statistcs> m = JsonConvert.DeserializeObject<List<Statistcs>>(jsonData);
        }

        public static bool AddStatistics(Statistcs m)
        {
            var conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\EvgenieL\Source\Repos\XO\XO\WebApplication\App_Data\XO.mdf;Integrated Security=True");
            var command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = $"INSERT INTO Statistic(Date, Result, StepCounter, UserFirst) VALUES('{m.Date}', '{m.Result}', {m.StepCounter}, '{m.UserFirst}')";

            try
            {
                conn.Open();
                command.ExecuteNonQuery();
            }
            catch
            {
                return false;
            }
            finally
            {
                conn.Close();
            }

            return true;
        }
    }
}

Tell me what the error can be and how to fix that's what it gives?
6bed8c8fdfdf46c395b58528f861b2d4.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
one pavel, 2017-06-14
@onepavel

I'll try to guess,
the Database.SetInitializer method takes something like this IDatabaseInitializer,
and you slip it a StaticDbInitializer inherited from Object or the like.

B
basrach, 2017-06-24
@basrach

The compiler says that it cannot understand what type parameter (generic type parameter) should be substituted instead of TContext in the SetInitializer method and asks to specify this TContext explicitly. But you don't really need to specify it explicitly, you need to make StaticDbInitializer implement the IDatabaseInitializer interface, where the context should probably also be inherited from some DataContext.
The first step to take is to write the class declaration as:
public class StaticDbInitializer : IDatabaseInitializer
{
....
Where MyContext is the class that represents... some context
PS Localized IDE is evil

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question