![](/img/trans.png)
[英]Different versions of database and SQL Server; Couldn't find SQL Server
[英]Can't find SQL Server database
我的代码有2个问题。
我使用代码优先方法创建了一个数据库,并且代码成功运行,但是当我打开SQL Server Management Studio时,找不到数据库。
然后由于某种原因,我更改了学生班级的名称(“首字母大写”),然后在尝试运行它时显示错误。
学生班:
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属性
我添加的数据库不存在:
任何帮助将不胜感激。 我是一个初学者,如果您还不能说出来的话。.所以请比平时更加精巧。 非常感谢您的宝贵时间。
我发现为什么我的数据库不在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
文件中未提供连接字符串属性,我仍然想知道数据将保存在哪里。
如果我再次更改学生姓名,是否会像以前一样收到错误消息? 如果可以,该如何解决?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.