简体   繁体   English

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

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

I'm trying to create the database using EF Code First approach. 我正在尝试使用EF Code First方法创建数据库。 I have a DataAccessLayer and a DomainLayer in the following way : 我通过以下方式有一个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; }
   }
}

And I create a Console application to test the app in the following way : 然后,我创建一个控制台应用程序,以通过以下方式测试该应用程序:

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();
         }
       }
     }
}

But when I run the app it gave me the following error : 但是,当我运行该应用程序时,它给了我以下错误:

Exception: 例外:

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.

Message: 信息:

The provider did not return a string ProviderManifestToken.

InnerException: 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)

I'm have installed in my machine SQL Server 2005 too. 我的计算机上也安装了SQL Server 2005。 But according to book I'm reading : 但是根据书,我正在阅读:

Code First used the information it discovered in the Destination and Lodging classes to determine the model and infer the schema of the database that these classes are persisted in. Since we provided no connection string information, it uses its default convention, which is to look in the local SQL Server Express instance (localhost\\SQLEXPRESS) for a database matching the fully qualified name of the context class DataAccess.BreakAwayContext. Code First使用在Destination和Lodging类中发现的信息来确定模型并推断这些类将保留在其中的数据库模式。由于我们未提供连接字符串信息,因此它使用其默认约定,即查找与上下文类DataAccess.BreakAwayContext的完全限定名称匹配的数据库的本地SQL Server Express实例(localhost \\ SQLEXPRESS)。 Not finding one, Code First creates the database and then uses the model it discovered by convention to build the tables and columns of the database. 没有找到一个,Code First创建数据库,然后使用约定发现的模型来构建数据库的表和列。

EDIT 编辑

My app.config file it's in the BreakAwayConsole project and have the following lines : 我的app.config文件位于BreakAwayConsole项目中,具有以下几行:

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

Any help is appreciated. 任何帮助表示赞赏。

You are going to need to add the connection string information to the app.config file. 您将需要将连接字符串信息添加到app.config文件中。 I think you're config file should look something like this 我认为您的配置文件应该看起来像这样

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

I had very similar problem. 我有非常类似的问题。 I as not able to connect to .\\SQLEXPRESS. 我无法连接到。\\ SQLEXPRESS。 I managed to get it working by reinstalling SQLEXPRESS with a named instance of SQLEXPRESS from: http://www.microsoft.com/en-au/download/details.aspx?id=29062 . 我设法通过使用SQLEXPRESS的命名实例重新安装SQLEXPRESS来使它正常工作: http : //www.microsoft.com/en-au/download/details.aspx?id= 29062。 I could then connect successfully to .\\SQLEXPRESS and then ef codefirst worked without any connection strings in the app.config. 然后,我可以成功连接到。\\ SQLEXPRESS,然后ef代码首先工作,而app.config中没有任何连接字符串。

Having dealt with various connection string , I solved my problem with the following : 处理了各种连接字符串后,我通过以下方法解决了我的问题:

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