实体框架代码创建数据库的第一个问题

时间:2014-05-02 18:43:04

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

我尝试使用EF Code First方法创建数据库。我通过以下方式获得DataAccessLayerDomainLayer

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.

的InnerException:

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>

感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

您需要将连接字符串信息添加到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>

答案 1 :(得分:0)

我有类似的问题。我无法连接到。\ SQLEXPRESS。我设法通过从http://www.microsoft.com/en-au/download/details.aspx?id=29062重新安装带有SQLEXPRESS命名实例的SQLEXPRESS来实现它。然后我可以成功连接到。\ SQLEXPRESS,然后ef codefirst在app.config中没有任何连接字符串。

答案 2 :(得分:0)

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

<?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>