简体   繁体   中英

Can't find SQL Server database

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:

在此处输入图片说明

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM