简体   繁体   English

实体框架代码首先在SQL Server Management Studio中没有数据库

[英]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: 我做了以下工作:

  • Created the classes 创建课程
  • Created a data context using said classes 使用上述类创建数据上下文
  • I used parameterless constructor on context class with :base("connectionstring") 我在上下文类上使用了无参数构造函数:base("connectionstring")
  • I opened the Nuget Manager console and i typed the commands: 我打开了Nuget Manager控制台,并输入了以下命令:

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.

相关问题 实体框架或SQL Server Management Studio? - Entity Framework or SQL Server Management Studio? 如何使用实体框架生成与SQL Server兼容的数据库(代码优先) - How to generate a SQL Server compatible database with Entity Framework (code first) 实体框架linq to entity在代码上速度慢但在SQL Server Management Studio中速度很快 - Entity Framework linq to entity slow on code but fast in SQL Server Management Studio 实体框架数据库优先-SQL Server - Entity Framework database-first with SQL Server 外部SQL服务器上的实体框架代码优先 - Entity Framework Code First on external SQL server C#实体框架db.Database.SqlQuery sql like将不同的行返回到sql server management studio - C# Entity Framework db.Database.SqlQuery sql like returns different rows to sql server management studio Code First 迁移和 SQL Server Management Studio 的问题 - Problems with Code First Migration and SQL Server Management Studio 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 将数据库从嵌入式更改为SQL Server Express后,Entity Framework 4.1代码第一个模型兼容性问题 - Entity Framework 4.1 code first model compatibility issue after changing database from embedded to SQL Server Express MVC4代码优先实体框架将大文件上传到SQL Server数据库 - MVC4 Code First Entity Framework Upload large files to a SQL Server database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM