2014-05-24 59 views
1

我知道這個問題必須先回答,但我找不到解決方案,我的情況。 我在Visual Studio 2013中創建了一個基於服務的數據庫,並且我想連接到這個數據庫。 我創建了dbSet,從的DbContext類heritating並添加entitiframework參考一切都很好,除了ConnectionString的代碼第一個實體框架確實無法連接到數據庫

<connectionStrings> 
<add name ="master" connectionString="Data Source=(localdb)\v11.0; 
         AttachDbFilename=c:\users\reda\documents\visual studio 2013\Projects\Testing new things\Testing new things\master.mdf; 
         Integrated Security=True;"/> </connectionStrings> 

當我運行我的程序,我收到了Data.Entity.Core.ProviderIncompatibleException例外;這意味着它無法訪問數據庫,我在哪裏錯了?

btw,是否有任何其他解決方案來處理本地數據庫?

的錯誤信息是:

Unhandled Exception: System.Data.Entity.Core.ProviderIncompatibleException: An e 
rror occurred accessing the database. This usually means that the connection to 
the database failed. Check that the connection string is correct and that the ap 
propriate DbContext constructor is being used to specify it or find it in the ap 
plication's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for i 
nformation on DbContext and connections. See the inner exception for details of 
the failure. ---> System.Data.Entity.Core.ProviderIncompatibleException: The pro 
vider did not return a ProviderManifestToken string. ---> System.Data.SqlClient. 
SqlException: A network-related or instance-specific error occurred while establ 
ishing a connection to SQL Server. The server was not found or was not accessibl 
e. Verify that the instance name is correct and that SQL Server is configured to 
allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local 
Database Runtime error occurred. The specified LocalDB instance does not exist. 
) 
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception 
, Boolean breakConnection, Action`1 wrapCloseInAction) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj 
ect stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
    at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternal 
ConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Bool 
ean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFa 
ilover) 
    at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo 
serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSn 
iOpenTimeout, TimeoutTimer timeout, Boolean withFailover) 
    at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo 
serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirect 
edUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, 
TimeoutTimer timeout) 
    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTime 
r timeout, SqlConnectionString connectionOptions, SqlCredential credential, Stri 
ng newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent 
ity identity, SqlConnectionString connectionOptions, SqlCredential credential, O 
bject providerInfo, String newPassword, SecureString newSecurePassword, Boolean 
redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData r 
econnectSessionData) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp 
tions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConn 
ectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) 

    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn 
ectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConne 
ctionPoolKey poolKey, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owning 
Object, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) 
    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection o 
wningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection 
) 
    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow 
ningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean o 
nlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& co 
nnection) 
    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow 
ningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbCon 
nectionInternal& connection) 
    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection 
owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions 
, DbConnectionInternal oldConnection, DbConnectionInternal& connection) 
    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(Db 
Connection outerConnection, DbConnectionFactory connectionFactory, TaskCompletio 
nSource`1 retry, DbConnectionOptions userOptions) 
    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection 
outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 
retry, DbConnectionOptions userOptions) 
    at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 re 
try) 
    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) 
    at System.Data.SqlClient.SqlConnection.Open() 
    at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Ope 
n>b__38(DbConnection t, DbConnectionInterceptionContext c) 
    at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispat 
ch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TIntercepti 
onContext interceptionContext, Action`3 executing, Action`3 executed) 
    at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open 
(DbConnection connection, DbInterceptionContext interceptionContext) 
    at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass31.<Usin 
gConnection>b__2f() 
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass 
1.<Execute>b__0() 
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](
Func`1 operation) 
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action op 
eration) 
    at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnect 
ion sqlConnection, Action`1 act) 
    at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbC 
onnection sqlConnection, Action`1 act) 
    at System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifestToke 
n(DbConnection connection) 
    at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken 
(DbConnection connection) 
    --- End of inner exception stack trace --- 
    at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken 
(DbConnection connection) 
    at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManif 
estTokenChecked(DbProviderServices providerServices, DbConnection connection) 
    --- End of inner exception stack trace --- 
    at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManif 
estTokenChecked(DbProviderServices providerServices, DbConnection connection) 
    at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__Displa 
yClass1.<ResolveManifestToken>b__0(Tuple`3 k) 
    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Fu 
nc`2 valueFactory) 
    at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveMani 
festToken(DbConnection connection) 
    at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConn 
ection connection, DbProviderManifest& providerManifest) 
    at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) 
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalCo 
ntext internalContext) 
    at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) 
    at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 
    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.get_InternalContext() 
    at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, Ent 
ityState newState, Object entity, String methodName) 
    at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) 
    at System.Data.Entity.DbSet`1.Add(TEntity entity) 
    at Testing_new_things.Program.Main(String[] args) in c:\Users\Reda\Documents\ 
Visual Studio 2013\Projects\Testing new things\Testing new things\Program.cs:lin 
e 19 
Press any key to continue . . . 

回答

0

連接字符串缺少提供商的信息。將其更改爲實際的連接字符串太在這

<add name ="master" connectionString="Data Source=(localdb)\v11.0; 
        AttachDbFilename=c:\users\reda\documents\visual studio 2013\Projects\Testing new things\Testing new things\master.mdf; 
        Integrated Security=True;" 
providerName="System.Data.SqlClient"/> 
+0

您可能還缺少'初始Catalog'財產。 – DavidG

+0

我以前添加了providerName並且沒有工作,並且初始目錄也是。並且不起作用 – Redaa

+0

什麼是確切的錯誤信息? – DavidG

相關問題