【问题标题】:Can't find SQL Server database找不到 SQL Server 数据库
【发布时间】:2016-09-18 23:52:16
【问题描述】:

我的代码有 2 个问题。

  1. 我使用代码优先方法创建了一个数据库,代码运行成功,但是当我打开 SQL Server Management Studio 时,我找不到数据库。

  2. 然后由于某种原因我更改了学生班级中的名称(“将首字母设为大写”),然后当我尝试运行它时,它显示错误。

学生班:

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; }
}

标准类:

class Standard
{
    public Standard()
    {
    }

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

上下文类:

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

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

主类:

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();
        }
    }
}

cxt.Students.Add(st); 处的错误消息:

EntityFramework.dll 中出现“System.InvalidOperationException”类型的未处理异常

附加信息:应用程序配置文件中的连接字符串“StudentContext”不包含所需的 providerName 属性

我添加的数据库不存在:

任何帮助将不胜感激。我是一个初学者,如果你还不能说......所以请比你平时更详细一点。非常感谢您的宝贵时间。

【问题讨论】:

  • 你的配置文件中StudentContextproviderName是什么?
  • 你有connection string 吗?你能显示你的connection string 吗?
  • 这是我的连接字符串

标签: c# sql-server entity-framework-6 dbcontext sql-server-2012-localdb


【解决方案1】:

我发现为什么我的数据库不在 SQL Server Management Studio 中是因为我没有在 app.config 文件中提供连接字符串。

我补充说:

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

这在 SQL Server Management Studio 中创建了一个数据库名称 Sample,但我仍然想知道如果我没有在 app.config 文件中提供连接字符串属性,数据会保存在哪里。

如果我再次更改学生姓名,我会像以前一样收到错误吗?如果是这样,我该如何解决?

【讨论】:

  • SQL Server Management Studio 是用于处理 SQL Server 数据库的管理 GUI,而 SSMS 什么也不做。所有 SQL 数据库都通过它的引擎存储在 SQL Server 中,并且可以放在任何地方。如果你看不到你的第一个数据库,那么有两种可能的情况,1.你的第一个连接字符串指向 SQL Express,你的数据库存储在本地项目文件中,2.你的数据库存储在“系统数据库 > 主”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 1970-01-01
相关资源
最近更新 更多