Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在实体框架codefirst方法中创建的数据库的默认物理位置_C#_Entity Framework_Datacontext - Fatal编程技术网

C# 在实体框架codefirst方法中创建的数据库的默认物理位置

C# 在实体框架codefirst方法中创建的数据库的默认物理位置,c#,entity-framework,datacontext,C#,Entity Framework,Datacontext,我正在学习实体框架。我使用实体框架代码优先的方法创建了一个演示应用程序(没有指定任何连接字符串) 控制台应用程序运行良好,它创建了数据库,我可以从中获取数据。但是,我无法从身体上看到它。我是说我想知道它是在哪里被创造出来的?在本地Sql server或Visual studio本地数据库中 App.Config <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections&g

我正在学习实体框架。我使用实体框架代码优先的方法创建了一个演示应用程序(没有指定任何连接字符串)

控制台应用程序运行良好,它创建了数据库,我可以从中获取数据。但是,我无法从身体上看到它。我是说我想知道它是在哪里被创造出来的?在本地Sql server或Visual studio本地数据库中

App.Config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

上下文类

using System.Data.Entity;
using BlogsApp;

namespace DataLayer
{
    public class Context:DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }
}
使用System.Data.Entity;
使用BlogsApp;
命名空间数据层
{
公共类上下文:DbContext
{
公共数据库集博客{get;set;}
公共DbSet Posts{get;set;}
}
}

我正在使用EntityFramework 6.1.1、.Net Framework 4.5,Visual studio 2012。我的机器上安装了SqlServer 2012。请帮助我查找数据库,它位于您的SQLEXPRESS实例中。 可能会被称为ConsoleApplication1.Context。 如果您有sql server studio manager,请使用。\SQLEXPRESS作为服务器名称,您将在那里看到数据库。您可以从那里获取文件位置,但应位于

C:\ProgramFiles(x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data

C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data

如果您运行的是32位windows

从这里

“如果安装了SQL Express(包含在Visual Studio 2010中),则会在本地SQL Express实例(.\SQLEXPRESS)上创建数据库。如果未安装SQL Express,则代码将首先尝试使用LocalDb((LocalDb)\v11.0)-LocalDb包含在Visual Studio 2012中”

从这里


根据您的配置文件,您的默认连接工厂是:

System.Data.Entity.Infrastructure.SqlConnectionFactory

这是SQL Server的连接工厂。由于您没有提供连接字符串,实体框架将尝试连接到名为
\SQLEXPRESS
的SQL Server实例,并使用上下文的完整类型名
DataLayer.context
创建一个数据库

您可以通过更改默认连接工厂和调用
DbContext
构造函数的特殊重载来更改这两个默认值,但通常没有人会介意

您通常应该做的是添加一个以您的上下文命名的连接字符串:

<connectionStrings> 
    <add name="Context"  
         providerName="System.Data.SqlClient"  
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Blogging;Integrated Security=True;MultipleActiveRecordSets=True"/> 
</connectionStrings>


在没有指定连接字符串和默认SQLServerExpress实例的情况下,EntityFramework会在用户文件夹(例如C:\Users[yourusername])中创建sql数据库文件,其名称与您的项目一致


这些文件似乎没有绑定到SQL Server,即您可以自由移动它们。

您可以显示app.config吗?添加了app.config和Context类“名称与您的项目一致”。这条路你说得对,但名字似乎是。从用于访问数据库的DbContext派生的类型(至少对于(localdb)\MSSQLLocalDB)。因此,在OP的情况下,路径应该是:
C:\Users\[yourusername]\DataLayer.Context.mdf
(或者更好地使用环境变量进行泛化:%HomePath%\DataLayer.Context.mdf`)
<connectionStrings> 
    <add name="Context"  
         providerName="System.Data.SqlClient"  
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Blogging;Integrated Security=True;MultipleActiveRecordSets=True"/> 
</connectionStrings>
<connectionStrings> 
    <add name="Context"  
         providerName="System.Data.SqlClient"  
         connectionString="Data Source=(localdb)\v11.0;Initial Catalog=Blogging;Integrated Security=True;MultipleActiveRecordSets=True"/> 
</connectionStrings>