[英]Entity Framework Code First no database in SQL Server Management Studio
我试图首先使用EF代码创建一个数据库。 我做了以下工作:
:base("connectionstring")
启用迁移
添加迁移mydatabase
更新数据库
完成此操作后,包含迁移文件的文件夹将出现在解决方案资源管理器中,但数据库仍未出现在我的SQL Server Management Studio中
下面是代码:
1.类
public class Kid
{
public int KidId { get; set; }
public int Age { get; set; }
public string Name { get; set; }
}
[Table("School")]
public class School
{
[Key]
public int SchoolId { get; set; }
public string SchoolName { get; set; }
public List<Kid> Kids { get; set; }
}
public class SchoolContext:DbContext
{
public SchoolContext() : base("SchoolContext") { }
public DbSet<Kid> Kid { get; set; }
public DbSet<School> School { get; set; }
}
2.主要
class Program
{
static void Main(string[] args)
{
SchoolContext myDb = new SchoolContext();
Kid mykid = new Kid { Age = 14, KidId = 2, Name = "Adrian" };
Kid mykid2 = new Kid { Age = 16, KidId = 3, Name = "Adriansan" };
School mySchool = new School { Kids = new List<Kid>{ mykid, mykid2 }, SchoolId = 73, SchoolName = "Iovan Ducici" };
myDb.Kid.Add(mykid);
myDb.School.Add(mySchool);
myDb.SaveChanges();
Console.ReadLine();
}
}
我怀疑App.config文件中有一些要做的事情,该文件的connectionString标记为空,但是我不知道它必须完成什么。
通常,您需要在应用程序配置中具有一个连接字符串,该字符串知道您在何处创建数据库。 上下文只是创建数据库的蓝图,“ SchoolContext”应在应用程序配置中引用连接字符串,或者类似于运行EF Code First的实际项目
<connectionStrings>
<add name="SchoolContext" providerName="System.Data.SqlClient" connectionString="Server=.;Database=SchoolContext;Integrated Security=True;"/>
</connectionStrings>
如果您没有此功能,则很可能无法使用。 而且由于您首先要使用EF代码手动进行大量操作,因此可能不会显示。 或者,它可能假定使用MS默认数据库来处理与MS SQL不同的连接,或者它可能是SQL Express。
我从这个站点上获得了本教程,这对于学习EF Code First的基础知识是很了不起的: http : //www.entityframeworktutorial.net/code-first/entity-framework-code-first。 aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.