0

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();
    }
  • Check [these](https://stackoverflow.com/questions/22680342/value-cannot-be-null-parameter-name-entityset) – Steve Greene Jun 19 '19 at 20:27
  • Declared all of the classes in the context but the error still persists, I started to think that I messed up connection string – John Davison Jun 19 '19 at 22:11
  • + Local 'context.Amenitisi.Local' threw an exception of type 'System.InvalidOperationException' System.Collections.ObjectModel.ObservableCollection {System.InvalidOperationException} Got this weird error as well in local database, tried changing Korisnik to Amenitis(this class has only 2 properties string and int) I thought it's because of enums I use in Korisnik but apparently it's not – John Davison Jun 19 '19 at 22:17
  • Solved it I added [NotMaped] to HttpPostedFileBase – John Davison Jun 20 '19 at 00:13
  • I would upvote that answer in the link. – Steve Greene Jun 20 '19 at 00:22

0 Answers0