2013-09-03 75 views
7

Tôi có Visual Studio 2012 và tôi đang sử dụng ngăn xếp Khuôn khổ thực thể với EF 6. Tôi đã làm tất cả chính xác nhưng trong khi thêm di chuyển, tôi nhận được lỗi .Entity Framework 6 với SQL Server 2012 cung cấp cho System.Data.Entity.Core.ProviderIncompatibleException

System.Data.Entity.Core.ProviderIncompatibleException

Dưới đây là các lớp

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

File bối cảnh

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

Và phần file web.config cho connctionstring

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

Tôi nhận lỗi bất cứ khi nào Tôi đang cố gắng g để thêm di chuyển dưới dạng:

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

Bạn không nên sử dụng 'base (" name = ShoppingCartDb ")' thay vì 'base (" ShoppingCartDb ")'? – Pawel

Trả lời

21

Hãy thử điều này. Hãy chắc chắn rằng dự án mà ShoppingCartContext của bạn đang ở trong, là dự án khởi động hoặc khi thực hiện lệnh thêm-di chuyển bao gồm tham số -startupprojectname ex. add-migration -startupprojectname yourprojectname

+1

Thiết lập dự án WebApi là "Dự án khởi động" là giải pháp cho vấn đề của tôi. Nắm bắt tốt! – ilter