Question

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.

1
0
9/19/2016 4:15:02 AM

Popular Answer

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?

1
9/19/2016 4:16:40 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow