سؤال

I'm trying to create the database using EF Code First approach. I have a DataAccessLayer and a DomainLayer in the following way :

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:

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

EDIT

My app.config file it's in the BreakAwayConsole project and have the following lines :

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

Any help is appreciated.

هل كانت مفيدة؟

المحلول 3

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>

نصائح أخرى

You are going to need to add the connection string information to the app.config file. 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. 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 . I could then connect successfully to .\SQLEXPRESS and then ef codefirst worked without any connection strings in the app.config.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top