[英]Entity Framework Code First no database in SQL Server Management Studio
I am trying to create a DB using EF code first. 我试图首先使用EF代码创建一个数据库。 I did the following:
我做了以下工作:
:base("connectionstring")
:base("connectionstring")
enable-migrations 启用迁移
add-migrations mydatabase 添加迁移mydatabase
update-database 更新数据库
After doing this the folder with the migration files appeared in the solution explorer but the Database still doesn't appear in my SQL Server Management Studio 完成此操作后,包含迁移文件的文件夹将出现在解决方案资源管理器中,但数据库仍未出现在我的SQL Server Management Studio中
Below is the code: 下面是代码:
1.Classes 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.Main 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();
}
}
I suspect there's something to be done in the App.config file which has a connectionString tag which is empty but I don't know what it has to be completed. 我怀疑App.config文件中有一些要做的事情,该文件的connectionString标记为空,但是我不知道它必须完成什么。
Generally you need to have a connection string in the app config that knows 'where' is your database created at. 通常,您需要在应用程序配置中具有一个连接字符串,该字符串知道您在何处创建数据库。 A context is just a blueprint to create the database that 'SchoolContext' should be referencing a connection string in an app config or similar to an actual project that runs the EF Code First
上下文只是创建数据库的蓝图,“ SchoolContext”应在应用程序配置中引用连接字符串,或者类似于运行EF Code First的实际项目
<connectionStrings>
<add name="SchoolContext" providerName="System.Data.SqlClient" connectionString="Server=.;Database=SchoolContext;Integrated Security=True;"/>
</connectionStrings>
If you do not have this it will most likely not work. 如果您没有此功能,则很可能无法使用。 And since you do a lot of stuff manually with EF code first it may not show up.
而且由于您首先要使用EF代码手动进行大量操作,因此可能不会显示。 Or it may assume an MS default database which handles connections differently than MS SQL or it may be SQL Express.
或者,它可能假定使用MS默认数据库来处理与MS SQL不同的连接,或者它可能是SQL Express。
I took this tutorial from this site a while back and it is fantastic for learning the ins and outs of the basics for EF Code First: http://www.entityframeworktutorial.net/code-first/entity-framework-code-first.aspx 我从这个站点上获得了本教程,这对于学习EF Code First的基础知识是很了不起的: http : //www.entityframeworktutorial.net/code-first/entity-framework-code-first。 aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.