0

I have 2 problems with my code.

  1. I created a database using code first method and the code ran successfully but when I opened SQL Server Management Studio, I couldn't find the database.

  2. Then for some reason I changed the names in the student class ("made first letters capital") and then when I tried to run it, it shows an error.

Student class:

class Student
{
    public Student()
    {
    }

    public int StudentID {get;set;}
    public string StudentName { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }

    public Standard Standards { get; set; }
}

Standard class:

class Standard
{
    public Standard()
    {
    }

    public int StandardID { get; set; }
    public string StandardName { get; set; }
    public ICollection<Student> students { get; set; }
}

Context Class:

class StudentContext : DbContext
{
    public StudentContext() : base()
    {
    }

    public DbSet<Standard> Standards { get;set;}
    public DbSet<Student> Students { get; set; }
}

Main class:

class Program
{
    static void Main(string[] args)
    {
        using (var cxt = new StudentContext())
        {
            Student st = new Student() { StudentName = "Vineeth reddy" };
            cxt.Students.Add(st);
            cxt.SaveChanges();
        }
    }
}

Error message at cxt.Students.Add(st);:

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

Additional information: The connection string 'StudentContext' in the application's configuration file does not contain the required providerName attribute

The database I added isn't there:

enter image description here

Any help would be highly appreciated. I'm a beginner, if you can't tell already.. so please be a little more elaborate than you usually are. Thank you very much for your time.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vineeth
  • 114
  • 1
  • 14

1 Answers1

1

I figured out why my database wasn't in SQL Server Management Studio is because I didn't provide the connection string in the app.config file.

I added:

<ConnectionStrings>
    <add name="StudentContext"
         connectionString = "server=.; database = Sample; integrated security = true"
         providerName = "system.Data.sqlClient"/>
</ConnectionStrings>

This made a database names Sample in SQL Server Management Studio, but I still would like to know where the data would have been saved if I didn't provide a connection string attribute in app.config file.

And if I change the student names again, will I get an error like before? If I do, how can I solve it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vineeth
  • 114
  • 1
  • 14
  • 1
    SQL Server Management Studio is the management GUI to work with a SQL Server database and SSMS does nothing else. All SQL databases stored in SQL Server via it's engine and may placed anywere. If you cant see your first database then there are two possible things, 1. your first connection string point to SQL Express and your database stored in local project files and 2. your database stored in 'System Databases > master' – n.y Sep 19 '16 at 04:44