mercredi 19 juin 2019

How to properly connect database with MVC web project, getting Value cannot be null. Parameter name: entitySet'

When I add something to the DbSet I get the error I described, I imported everything I need plus some things I read on stackoverflow, when I make a console app I connect it without a problem but for some reason, it doesn't work with web project.

This is part of my Web.config file I provided the rest of the classes below

<add name="MyDBContext" providerName="System.Data.SqlClient" connectionString="Server=Batonja-PC; Database=ProjekatBaza; user=sa;password=stoka123"/>

public class Korisnik {

public Korisnik(){

}

public override bool Equals(object obj)
{
    Korisnik gost = (Korisnik)obj;
    if(Username == gost.Username)
    {
        return true;
    }
    return false;
}

public override int GetHashCode()
{
    return Username.GetHashCode();
}

public override string ToString()
{
    return Ime + " " + Prezime + " " + Pol.ToString() + " " + Username + " " + Password;

}



public Korisnik(string ime, string password, Pol? pol, string prezime, string username)
{
    Ime = ime;
    Password = password;
    Pol = pol;
    Prezime = prezime;
    Username = username;
    Uloga = Roles.Domacin;
}



public string Ime { get; set; }

public string Password { get; set; }

public Pol? Pol { get; set; }

public string Prezime { get; set; }

public Roles? Uloga { get; set; }

[Key]
public string Username { get; set; }

}

public class MyDBContext : DbContext
{
    public DbSet<Domacin> Domacini { get; set; }
    public DbSet<Gost> Gosti { get; set; }
    public DbSet<Administrator> Administratori { get; set; }
    public DbSet<Rezervacija> Rezervacije { get; set; }
    public DbSet<Komentar> Komentari { get; set; }
    public DbSet<Apartman> Apartmani { get; set; }
    public DbSet<Korisnik> Korisnici { get; set; }

    public MyDBContext(): base("MyDBContext")
    {

    }


}

public ActionResult Index() {

        UcitajAdmine(@"D:\FAKS\WEB\Projekat\Projekat\Projekat\Administratori.txt");

        using (var context = new MyDBContext())
        {
            Korisnik korisnik = new Korisnik("Mile","stokan",Pol.Muski,"MIlic","Milo");
            try
            {
                context.Korisnici.Add(korisnik);
            }catch(SqlException ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            context.SaveChanges();
        }

            return View();
    }




Aucun commentaire:

Enregistrer un commentaire