Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2010 如何使用EF和MVC 3连接到SQL Server 2008 R2_Visual Studio 2010_Asp.net Mvc 3_Sql Server 2008 R2 - Fatal编程技术网

Visual studio 2010 如何使用EF和MVC 3连接到SQL Server 2008 R2

Visual studio 2010 如何使用EF和MVC 3连接到SQL Server 2008 R2,visual-studio-2010,asp.net-mvc-3,sql-server-2008-r2,Visual Studio 2010,Asp.net Mvc 3,Sql Server 2008 R2,我只是想学习MVC3,并遵循Wrox的一本书。在我尝试做一个小练习之前,一切都很好。我遇到了一个很常见的错误。我什么都试过了,但找不到问题所在。谁能帮帮我吗 我的配置 安装VS201,然后使用WebPlatForm安装程序安装SP1。它在我的机器上安装了SP1和MVC3模板和框架。到目前为止还不错 之后我安装了SQLServer2008R2开发版,同样到目前为止还不错 我一步一步地跟着练习,发现了一个错误,这正成为我的噩梦。错误如下所述 System.Data.ProviderIncompati

我只是想学习MVC3,并遵循Wrox的一本书。在我尝试做一个小练习之前,一切都很好。我遇到了一个很常见的错误。我什么都试过了,但找不到问题所在。谁能帮帮我吗

我的配置

  • 安装VS201,然后使用WebPlatForm安装程序安装SP1。它在我的机器上安装了SP1和MVC3模板和框架。到目前为止还不错

  • 之后我安装了SQLServer2008R2开发版,同样到目前为止还不错

  • 我一步一步地跟着练习,发现了一个错误,这正成为我的噩梦。错误如下所述

    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: 
    
    这里还有连接字符串

    <add name="MusicStoreDB" connectionString="Server=(local); Database=MusicStore; Trusted_Connection=true; Integrated Security=True" providerName="System.Data.SqlClient" />
    
    
    
    编辑1: 我没有使用任何数据库来构建我的模型。我正在尝试使用EF的代码优先方法

    编辑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; }
        }
    }
    
    使用System.Data.Entity;
    命名空间MvcMusicStore.Models
    {
    公共类MvcMusicStoreContext:DbContext
    {
    //您可以将自定义代码添加到此文件。更改不会被覆盖。
    // 
    //如果希望实体框架删除并重新生成数据库
    //无论何时更改模型架构,都会自动添加以下内容
    //为Global.asax文件中的应用程序启动方法编写代码。
    //注意:这将在每次模型更改时销毁并重新创建数据库。
    // 
    //System.Data.Entity.Database.SetInitializer(新的System.Data.Entity.DropCreateDatabaseIfModelChanges());
    公共数据库集相册{get;set;}
    公共数据库集类型{get;set;}
    公共数据库集艺术家{get;set;}
    }
    }
    
    任何人都可以建议我试试


    谢谢

    您的EF连接字符串需要是一个

    在创建实体框架edmx的项目中检查app.config(如果使用数据库创建模型),其中应包含连接字符串

    下面是一些创建EF连接字符串的示例代码:

    efName
    是edmx文件的名称。示例:Test.edmx-efName=“Test”


    默认情况下,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; }
    }
    
    公共类MvcMusicStoreContext:DbContext
    {
    //将数据库连接的名称添加到基本DbContext Cals
    公共MvcMusicStoreContext():基(“MusicStoreDB”)
    {}
    公共数据库集相册{get;set;}
    公共数据库集类型{get;set;}
    公共数据库集艺术家{get;set;}
    }
    
    您的连接字符串:

    <add name="MusicStoreDB" connectionString="Data Source=localhost;Initial Catalog=MusicStoreDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
    

    只要用这个字符串替换你的
    conn
    字符串,如果你已经复制了MvcMusicStore.mdf文件,请从App_数据文件夹中删除这些文件,希望它能正常工作

    
    

    对于SQL Server 2008 R2 Developer Edition,您可以在电脑上使用以下连接模式

    马丁,不,我没有使用数据库来创建模型。方法是代码优先。您可以在这里找到一个示例-我可以看看您的数据库上下文类吗?(MyClass:DbContext)保罗,我为你编辑了我的帖子。请检查编辑2。请看下面我的答案,谢谢。保罗,运气不好。同样的错误。我尝试使用代码优先的方法,而且我没有使用SqlExpress版本。我通常使用开发者版本,当您使用SQLDeveloper时,您不会使用(本地),它将是一个命名实例。像YourName-PC一样。打开Management Studio,看看服务器名的值是多少。尝试更新到上面的连接。好的,我可以运行代码。谢谢保罗为解决我的问题所做的努力。我只是简单地重新安装了一切,甚至我的Win7操作系统,这次我安装的是SQLServer2008R2 Express,而不是SQLServer2008R2开发者。
    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"/>
    
    <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" />