简体   繁体   English

EF代码先找不到数据库

[英]EF Code first cannot locate database

I have created a service which is fetching some data and being stored into the database. 我创建了一个服务,该服务正在获取一些数据并将其存储到数据库中。 While fetching the data or storing the data, I can see data is being stored properly but I am unable to see the Database anywhere. 在获取数据或存储数据时,我可以看到数据已正确存储,但是无法在任何地方看到数据库。

My connection string and context are as follows 我的连接字符串和上下文如下

public class Context: DbContext 
{
    public DBContext() : base("name=DBConnectionString") 
    {
        public DbSet<IEmployee> Employees {get; set;}
    }
}


 <connectionStrings>
    <add name="DBConnectionString" 
    connectionString="Data Source=(localdb)\v11.0;Initial Catalog=SLDB;Integrated Security=true" 
    providerName="System.Data.SqlClient"/>
 </connectionStrings>

DB isn't appearing in SQL server object explorer neither I can find it on physical file system. 数据库也没有出现在SQL Server对象资源管理器中,我也无法在物理文件系统上找到它。

I have looked at following location for physical/mdf file already 我已经看过以下位置的物理/ MDF文件

  • c:\\user\\HaseebAsif c:\\ user \\ HaseebAsif
  • %AppData%(roaming/local)\\Microsoft.... %AppData%(漫游/本地)\\ Microsoft ....
  • c:\\Program Files\\Microsoft\\Microsoft sql server .... c:\\ Program Files \\ Microsoft \\ Microsoft sql服务器....

When I connect to server using '(localdb)/v11.0', it just shows the system database, not mine, SLDB. 当我使用'(localdb)/v11.0'连接到服务器时,它仅显示系统数据库,而不是我的SLDB。

Any idea how can I see the database in SSMS or in windows explorer 知道如何在SSMS或Windows资源管理器中查看数据库

Add AttachDbFilename=c:\\path\\todb\\SLDB.mdf to the connection string to both the project holding the EF context/models, etc. and the application itself. AttachDbFilename=c:\\path\\todb\\SLDB.mdf添加到包含EF上下文/模型等的项目以及应用程序本身的连接字符串中。

You can also use application domain properties in the connection string as such |TheVariable| 您也可以在连接字符串中使用应用程序域属性,例如|TheVariable|

Then in your Configuration.cs EF class module constructor: 然后在您的Configuration.cs EF类模块构造函数中:

public Configuration()
{
    AppDomain.CurrentDomain.SetData("TheVariable", @"c:\path\todb"); //or something dynamic...
}

Do the same at startup of your application and then you can it so: 在启动应用程序时执行相同的操作,然后可以这样做:

AttachDbFilename=|TheVariable|\SLDB.mdf

Do an Update-database again, the MDF file will be at the path specified in the connection string. 再次执行Update-database ,MDF文件将位于连接字符串中指定的路径。

If the database does not appear in SQL Management Studio at this point, right click the Database node and Attach the MDF manually. 如果此时数据库未出现在SQL Management Studio中,请右键单击“ Database节点,然后手动Attach MDF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM