【问题标题】:Entity Framework Code First issue to create the databaseEntity Framework Code First issue 创建数据库
【发布时间】:2014-05-02 18:43:04
【问题描述】:

我正在尝试使用 EF Code First 方法创建数据库。我有一个DataAccessLayer 和一个DomainLayer,方式如下:

namespace DomainLayer
{
   public class Lodging
   {
      public int LodgingId { get; set; }
      public string Name { get; set; }
      public string Owner { get; set; }
      public bool IsResort { get; set; }
      public Destination Destination { get; set; }
   }

   public class Destination
   {
      public int DestinationId { get; set; }
      public string Name { get; set; }
      public string Country { get; set; }
      public string Description { get; set; }
      public byte[] Photo { get; set; }

      public List<Lodging> Lodgings { get; set; }
   }
}

using System.Data.Entity;
using DomainLayer;

namespace DataAccessLayer
{
   public class BreakAwayContext : DbContext
   {
      public DbSet<Destination> Destinations { get; set; }
      public DbSet<Lodging> Lodgings { get; set; }
   }
}

我创建了一个控制台应用程序以通过以下方式测试该应用程序:

namespace BreakAwayConsole
{
    public class Program
    {
       public static void Main(string[] args)
       {
          InsertDestination();
       }

       private static void InsertDestination()
       {
          var destination = new Destination
         {
             Country = "Indonesia",
             Description = "EcoTourism at its best in exquisite Bali",
             Name = "Bali"
         };

         using (var context = new BreakAwayContext())
         {
            context.Destinations.Add(destination);
            context.SaveChanges();
         }
       }
     }
}

但是当我运行该应用程序时,它给了我以下错误:

例外:

An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

消息:

The provider did not return a string ProviderManifestToken.

内部异常:

A network-related or instance-specific error occurred while a connection to SQL Server is established. Server not found or was not accessible this. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server or Instance Specified)

我的机器上也安装了 SQL Server 2005。但根据我正在阅读的书:

Code First 使用它在 Destination 和 Lodging 类中发现的信息来确定模型并推断持久化这些类的数据库的模式。由于我们没有提供连接字符串信息,因此它使用其默认约定,即在本地 SQL Server Express 实例 (localhost\SQLEXPRESS) 中查找与上下文类 DataAccess.BreakAwayContext 的完全限定名称匹配的数据库。没有找到,Code First 创建数据库,然后使用它按照约定发现的模型来构建数据库的表和列。

编辑

我的app.config 文件位于BreakAwayConsole 项目中,并且包含以下几行:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

感谢任何帮助。

【问题讨论】:

  • 我这里猜测一下,你的datalayer项目的app.config中有连接字符串信息,但是你的控制台应用的app.config中没有连接字符串信息.您可以将数据层 app.config 中的连接字符串信息复制并粘贴到控制台 app.config 中,您应该已准备就绪。
  • @jrhutch 我的连接字符串在我的控制台应用程序项目中,而不是在数据层中
  • 连接字符串中的服务器名称是否与您在 SQL Management Studio 中看到的实例名称匹配?
  • App.config中connectionString的名称是什么?应该是 BreakAwayContext

标签: c# database entity-framework ef-code-first


【解决方案1】:

您需要将连接字符串信息添加到 app.config 文件中。我认为你的配置文件应该是这样的

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection " connectionString="Data Source=YourServerName;Initial Catalog=YourDatabaseName;Integrated Security=true;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【讨论】:

  • 除非他在上下文中指定连接字符串的名称,否则他会想将其命名为 DefaultConnection,对吧?
  • 为什么我需要手动定义连接字符串?? ...Code First 创建数据库,然后使用它按约定发现的模型
  • @Vkt0rS。它将尝试根据约定查找服务器,但如果失败,并且看起来它正在失败,那么显式是修复它的方法。
  • +1 to @Smith.h.Neil 是的,它确实需要是 DefaultConnection,我总是为我的 dbContext 创建一个默认构造函数来指定我的连接字符串名称。 Public MyDbContext() : base("MyDbContext") {//any config} 更新了我的答案
  • @Vkt0rS。在查看了您的问题和书中的引文后,我认为问题在于您的 SQL 实例是默认实例 (localhost) 而不是 localhost\SQLExpress。无论哪种方式,我都建议在 app.config 中明确定义连接。
【解决方案2】:

我有非常相似的问题。我无法连接到 .\SQLEXPRESS。我设法通过使用 SQLEXPRESS 的命名实例重新安装 SQLEXPRESS 来使其工作:http://www.microsoft.com/en-au/download/details.aspx?id=29062。然后我可以成功连接到 .\SQLEXPRESS,然后 ef codefirst 在 app.config 中没有任何连接字符串的情况下工作。

【讨论】:

    【解决方案3】:

    处理了各种连接字符串后,我用以下方法解决了我的问题:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup>
           <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
    
        <connectionStrings>
          <add name="BreakAwayContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=BreakAwayContext; Integrated Security=True;Trusted_Connection=true" providerName="System.Data.SqlClient"/>
        </connectionStrings>
    </configuration>
    

    【讨论】:

    • 我以为您不想编辑 app.config - 如果您可以确保可以在本地连接到 .\sqlexpress,则不需要这样做
    • @ozidom SQLEXPRESS 默认没有Visual Studio??
    猜你喜欢
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    相关资源
    最近更新 更多