2013-09-03 2 views
7

У меня есть Visual Studio 2012, и я использую стек Entity Framework с EF 6. Я сделал все правильно, но при добавлении миграции я получаю ошибка .Entity Framework 6 с SQL Server 2012 дает System.Data.Entity.Core.ProviderIncompatibleException

System.Data.Entity.Core.ProviderIncompatibleException

Вот классы

public class Order 
{ 
    public virtual int OrderID { get; set; } 
} 

Контекст файла

public ShoppingCartContext() : base("ShoppingCartDb") 
{ 
     Database.SetInitializer<ShoppingCartContext>(new DropCreateDatabaseAlways<ShoppingCartContext>()); 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
     #region Entity Framework 6 RC-1 
     modelBuilder.Properties().Where(x => x.Name == x.DeclaringType.ToString() + "ID") 
       .Configure(x => x.IsKey()); 

     modelBuilder.Properties<DateTime>() 
       .Configure(x => x.HasColumnType("datetime2")); 
     #endregion 

     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
     base.OnModelCreating(modelBuilder); 
    } 

И секция файл web.config для connctionstring

<connectionStrings> 
    <add name="ShoppingCartDb" 
     connectionString="Server=Localhost;Database=ShoppingCartEfDb;User Id=sa;Password=xxxxxxxxxx" 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 

Я получаю ошибку всякий раз, Я пытаюсь g, чтобы добавить миграцию как:

System.Data.Entity.Core.ProviderIncompatibleException: 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. ---> System.Data.Entity.Core.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. --->

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. 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/Instance Specified)

+2

Не следует использовать 'base (" name = ShoppingCartDb ")' вместо 'base (" ShoppingCartDb ")'? – Pawel

ответ

21

Попробуйте это. Убедитесь, что проект, в котором находится ваш ShoppingCartContext, является проектом запуска или при выполнении команды add-migration включает параметр -startupprojectname ex. add-migration -startupprojectname yourprojectname

+1

Настройка проекта WebApi как «Startup Project» была решением моей проблемы. Хороший улов! – ilter