繁体   English   中英

实体框架代码创建数据库的第一期

[英]Entity Framework Code First issue to create the database

我正在尝试使用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类中发现的信息来确定模型并推断这些类将保留在其中的数据库模式。由于我们未提供连接字符串信息,因此它使用其默认约定,即查找与上下文类DataAccess.BreakAwayContext的完全限定名称匹配的数据库的本地SQL Server Express实例(localhost \\ SQLEXPRESS)。 没有找到一个,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>

任何帮助表示赞赏。

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

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

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

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

暂无
暂无

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

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