找不到我的实体框架数据库 [英] Cannot find my Entity Framework database

查看:189
本文介绍了找不到我的实体框架数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是code第一个实体框架数据库上有点糊涂了。

我创建了一个新的DbContext和阶级,我将存储在这种情况下,像这样的:

 命名空间MyProject.Subproject.Something
{
    公共类MyItem
    {
        公共字符串一个{获得;组; }
        公共字符串两个{获得;组; }
        [键]
        公共字符串三{获得;组; }
        公共字符串四{获得;组; }
    }

    公共类MyAppData:的DbContext
    {
        公共DbSet< MyItem> MyItems {获得;组; }
    }
}
 

我知道它的工作,因为我能做到这一点没有失败:

 变种DB =新MyAppData();
MyItem I =新MyItem();
// ...必填项
db.MyItems.Add(ⅰ);
db.SaveChanges();
 

此外,如果我重新启动我找到应用程序 db.MyItems.Count(),以反映该项目实际上是永久保存的地方。

我假设这是存储在的LocalDB ,因为我设置没有SQL Server数据库。我只希望能够做的就是查看表中的的LocalDB 的地方,但我似乎无法找到它。

如果我去数据源 - >添加新数据源 - >数据库 - >数据集 - >新建连接 - >微软SQL服务器,然后放在(的LocalDB)V11.0 服务器名称和下拉式数据库列表,我没有看到MyAppData或MyItems上市。

修改:我在我的App.config中看到的是< defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,的EntityFramework>


编辑2 :完整的App.config

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
  < configSections>
    <! - 有关实​​体框架配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468  - >
    <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本= 6.0.0.0,文化=中性公钥= XXXXXXXrequirePermission =FALSE/>
  < / configSections>
  <启动>
    < supportedRuntime版本=4.0版的SKU =NETFramework,版本= V4.5/>
  < /启动>
  <的EntityFramework>
    < defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,的EntityFramework>
      <参数>
        <参数值=mssqllocaldb/>
      < /参数>
    < / defaultConnectionFactory>
    <供应商>
      <供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
    < /供应商>
  < /的EntityFramework>
< /结构>
 

解决方案

您需要设置你的数据库服务器(的LocalDB)\ MSSQLLocalDB 。这是由以EF 6.1.1起了变化。所述MSSQLLocalDB中提到的参数。在变化的更多细节可以在这里找到的https://entityframework.$c$cplex.com/工作项/ 2246

MSSQLLocalDB 为SQL Server 2014年的默认实例名称,而 11.0 是SQL Server的默认实例名称2012

I am a little confused on the code first entity framework database.

I created a new DbContext and class that I will be storing in that context, like this:

namespace MyProject.Subproject.Something
{
    public class MyItem
    {
        public string One { get; set; }
        public string Two { get; set; }
        [Key]
        public string Three { get; set; }
        public string Four { get; set; }
    }

    public class MyAppData : DbContext
    {
        public DbSet<MyItem> MyItems { get; set; }
    }
}

I know that it's working since I can do this without failure:

var db = new MyAppData();
MyItem i = new MyItem();
// ... fill in item
db.MyItems.Add(i);
db.SaveChanges();

Additionally, if I restart the application I find db.MyItems.Count() to reflect that items are in fact persistently stored somewhere.

I'm assuming this is stored in localdb since I set up no SQL Server database. All I want to be able to do is see the table in localdb somewhere, however I can't seem to find it.

If I go to Data Sources -> Add New Data Source -> Database -> Data Set -> New Connection -> Microsoft SQL Server and then put in (localdb)v11.0 for the Server Name and dropdown the list of databases, I do not see MyAppData or MyItems listed.

Edit: What I see in my App.config is <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">


Edit 2: Full 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=xxxxxxx" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

解决方案

You need to set your database server as (LocalDB)\MSSQLLocalDB. This is a change made to EF 6.1.1 onwards. The MSSQLLocalDB is mentioned in your parameter. More details on the change can be found here https://entityframework.codeplex.com/workitem/2246

MSSQLLocalDB is the default instance name on SQL Server 2014 whereas v11.0 is the default Instance name on SQL Server 2012

这篇关于找不到我的实体框架数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆