How to open a password-protected SQL Server CE database using the Entity Framework

I am using EF 6.0 and SQL Server CE 4.0. The .sdf file is password protected, which I verified by opening the LinqPad file. When I try to open this database in code with the following connection string, I get an exception:

The specified password does not match the database password

code:

 using (var context = new MyDbContext("ExamManagement")) { context.Database.Initialize(false); } 

Connection string:

 <connectionStrings> <add name="ExamManagement" connectionString="Data Source=|DataDirectory|Pikeman.sdf;Max Database Size=4091;Password=123;" providerName="System.Data.SqlServerCe.4.0" /> </connectionStrings> 

enter image description here Stack trace:

in System.Data.Entity.Core.EntityClient.EntityConnection.Open ()
in System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection (Boolean shouldMonitorTransactions)
in System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T] (Func 1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery
1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery
1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery
1. <> c__DisplayClass7.b__5 ()
in System.Data.Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable 1 forMergeOption)
in System.Data.Entity.Core.Objects.ObjectQuery 1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator
1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator
1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator
1.MoveNext ()
in System.Linq.Enumerable.First [TSource] (IEnumerable`1 source)

+5
source share
1 answer

The connection string is fine (usually I do not specify Max Database Size, you can try to remove the parameter, but I'm sure this is not a problem). So, in your case, I think you are probably opening the database with a different password (as the exception suggests) or you are opening the wrong database. Try to specify the absolute path and open the database from this path, for example

 <connectionStrings> <add name="ExamManagement" connectionString="Data Source=C:\temp\Pikeman.sdf;Password=123;" providerName="System.Data.SqlServerCe.4.0" /> </connectionStrings> 
+1
source

Source: https://habr.com/ru/post/1266260/


All Articles