我只是想学习MVC 3并关注Wrox的一本书。事情很好,直到我尝试执行一个小练习。我收到一个很常见的错误。我已经尝试了一切,但无法找到问题所在。有人可以帮助我。
我的配置。
安装了VS201,然后使用WebPlatForm Installer安装SP1。在我的机器上安装了SP1并安装了MVC3模板和框架。到目前为止一切顺利。
之后我安装了SQL Server 2008 R2 Developer Edition,到目前为止一直很好。
我一步一步地练习,发现了一个错误,这对我来说变成了一场噩梦。错误在下面提到。
System.Data.ProviderIncompatibleException was unhandled by user code
Message=The provider did not return a ProviderManifestToken string.
Source=System.Data.Entity
StackTrace:
at System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
at System.Data.Entity.ModelConfiguration.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at System.Data.Entity.DbExtensions.Include[T](IQueryable`1 source, String path)
at System.Data.Entity.DbExtensions.Include[T,TProperty](IQueryable`1 source, Expression`1 path)
at MvcMusicStore.Controllers.StoreManagerController.Index() in c:\users\uk\documents\visual studio 2010\Projects\MvcMusicStore\MvcMusicStore\Controllers\StoreManagerController.cs:line 21
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException: System.Data.SqlClient.SqlException
Message=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)
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=20
LineNumber=0
Number=-1
Server=""
State=0
StackTrace:
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.SqlClient.SqlProviderServices.UsingConnection(SqlConnection sqlConnection, Action`1 act)
at System.Data.SqlClient.SqlProviderServices.UsingMasterConnection(SqlConnection sqlConnection, Action`1 act)
at System.Data.SqlClient.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection)
at System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
InnerException:
作为解决方案。我已经检查了SQL Server Browser服务是否已启用。我还从Microsoft执行了PortyQry.exe,以确定SQL Server是否正在响应。并收到以下回复。
Attempting to resolve name to IP address...
Name resolved to x.x.x.x
querying...
UDP port 1434 (ms-sql-m service): LISTENING or
Sending SQL Server query to UDP port 1434...
Server's response:
ServerName MVC3Machine
InstanceName MSSQLSERVER
IsClustered No
Version 10.50.1600.1
tcp 1433
♦ ♦
==== End of SQL Server query response ====
此处还有连接字符串
<add name="MusicStoreDB" connectionString="Server=(local); Database=MusicStore; Trusted_Connection=true; Integrated Security=True" providerName="System.Data.SqlClient" />
EDIT1: 我没有使用任何数据库来构建我的模型。我正在尝试使用EF的Code First方法。
编辑2:
using System.Data.Entity;
namespace MvcMusicStore.Models
{
public class MvcMusicStoreContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<MvcMusicStore.Models.MvcMusicStoreContext>());
public DbSet<Album> Albums { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<Artist> Artists { get; set; }
}
}
任何人都可以建议我尝试一下......
由于
答案 0 :(得分:4)
用这个替换你的conn
字符串,如果你复制了MvcMusicStore.mdf文件,那么从App_Data文件夹中删除它们希望它能工作
<clear />
<add name="MusicStoreEntities" connectionString="data source=.\SQLEXPRESS1;Integrated Security=SSPI;Initial Catalog=MvcMusicStore;AttachDBFilename=|DataDirectory|MvcMusicStore.mdf;User Instance=true" providerName="System.Data.SqlClient" />
答案 1 :(得分:2)
默认情况下,EFCF将尝试在您的本地./SQLEXPRESS上创建模型中的数据库。如果要在web.config中指定其他连接字符串,则必须在构造函数中告知EF连接字符串的名称你的数据库上下文如下:
public class MvcMusicStoreContext: DbContext
{
//Add the name of your database connection to the base DbContext calss
public MvcMusicStoreContext() : base("MusicStoreDB")
{}
public DbSet<Album> Albums { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<Artist> Artists { get; set; }
}
您的连接字符串:
<add name="MusicStoreDB" connectionString="Data Source=localhost;Initial Catalog=MusicStoreDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
答案 2 :(得分:1)
您的EF连接字符串必须是EntityConnection Connection String。
检查您在创建Entity Framework edmx的项目中的app.config(如果您使用数据库创建模型),并且应该有一个连接字符串。
以下是一些创建EF连接字符串的示例代码:
efName
是您的edmx文件的名称。示例:Test.edmx - efName =“Test”
private static string GetEntityFrameworkConnectionString(string connectionString, string efName)
{
var entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = connectionString;
entityBuilder.Metadata = "res://*/" + efName + ".csdl|res://*/" + efName + ".ssdl|res://*/" + efName + ".msl";
return entityBuilder.ToString();
}
答案 3 :(得分:0)
对于SQL Server 2008 R2 Developer Edition,您可以为您的pc使用以下连接字符串
<add name="MvcMusicStoreContext" connectionString="Data Source=MVC3Machine; Initial Catalog=MusicStoreDB; Integrated Security=SSPI;" providerName="System.Data.SqlClient" />