Hello
Exception when using SQLite, Entity FW and programmatically configuring
DBConfiguration.
(BTW it works perfectly fine if settings are done in app.config instead of
programmatic. But as a library developer I do not want to ask clients to put
some config settings in their app.config. I have also seen the way to
programmatically add config settings in application config at run-time but I do
not want to use that approach.)
Configuration code
public class SQLitDbProviderFactoryResolver : IDbProviderFactoryResolver
{
public DbProviderFactory ResolveProviderFactory(DbConnection
connection)
{
return System.Data.SQLite.SQLiteFactory.Instance;
}
}
public class SqliteConfiguration : DbConfiguration
{
public SqliteConfiguration()
{
SetDefaultConnectionFactory(new
System.Data.Entity.Infrastructure.SqlConnectionFactory());
SetProviderFactoryResolver(new SQLitDbProviderFactoryResolver());
{
SetProviderFactory("System.Data.SQLite",
SQLiteFactory.Instance);
SetProviderServices("System.Data.SQLite",
(DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
}
}
}
[DbConfigurationType(typeof(SqliteConfiguration))]
public class SqliteDbContext : DbContext
{
public virtual DbSet<Student> LibraryItems { get; set; }
public SqliteDbContext()
: base(new SQLiteConnection() { ConnectionString = "Data Source=
ReportingAppDb.sqlite" }, true)
{
try
{
var result = this.Database.SqlQuery<string>("SELECT name FROM
sqlite_master WHERE type='table' AND
name='SchemaInfo';").FirstOrDefault<string>();
}
catch(Exception ex)
{
throw;
}
}
}
The code in SqliteDbContext constructor in try catch block encounters exception.
Exception:
System.TypeInitializationException was unhandled by user code
HResult=-2146233036
Message=The type initializer for 'LibraryProject.StudentResources' threw an
exception.
Source=LibraryProject
TypeName=LibraryProject.StudentResources
StackTrace:
at LibraryProject.StudentResources.GetStudents()
at SQLiteAndEntity.MainWindow..ctor() in
c:\AjeyWork\Shared\SQLiteAndEntity\SQLiteAndEntity\MainWindow.xaml.cs:line 28
InnerException: System.Data.DataException
HResult=-2146233087
Message=An exception occurred while initializing the database. See the
InnerException for details.
Source=EntityFramework
StackTrace:
at
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action
action)
at
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at
System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext
c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput
input)
at
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1
action)
at
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.Initialize()
at
System.Data.Entity.Internal.LazyInternalContext.get_ObjectContext()
at
System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery[TElement](String
sql, Boolean streaming, Object[] parameters)
at
System.Data.Entity.Internal.InternalContext.ExecuteSqlQueryAsIEnumerator[TElement](String
sql, Boolean streaming, Object[] parameters)
at System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery(Type
elementType, String sql, Boolean streaming, Object[] parameters)
at
System.Data.Entity.Internal.InternalSqlNonSetQuery.GetEnumerator()
at System.Data.Entity.Infrastructure.DbRawSqlQuery`1.GetEnumerator()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1
source)
at LibraryProject.SqliteDbContext..ctor() in
c:\AjeyWork\Shared\SQLiteAndEntity\LibraryProject\SqliteDbContext.cs:line 75
at LibraryProject.StudentResources..cctor() in
c:\AjeyWork\Shared\SQLiteAndEntity\LibraryProject\SqliteDbContext.cs:line 97
InnerException: System.Data.Entity.Core.EntityCommandCompilationException
HResult=-2146232005
Message=An error occurred while preparing the command definition.
See the inner exception for details.
Source=EntityFramework
StackTrace:
at
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext
context, DbQueryCommandTree tree)
at
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext
context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption,
Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters,
AliasGenerator aliasGenerator)
at
System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1
forMergeOption)
at
System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__a()
at
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.<>c__DisplayClassb.<GetResults>b__9()
at
System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1
operation)
at
System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1
forMergeOption)
at
System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at
System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at
System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1
sequence)
at
System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1
query, Expression queryRoot)
at
System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression
expression)
at
System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression
expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1
source)
at
System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(Func`2
createContext)
at
System.Data.Entity.Internal.InternalContext.QueryForModelHash()
at
System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext
internalContext, ModelHashCalculator modelHashCalculator, Boolean
throwIfNoMetadata)
at
System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean
throwIfNoMetadata)
at System.Data.Entity.Database.CompatibleWithModel(Boolean
throwIfNoMetadata)
at
System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext
context)
at
System.Data.Entity.Internal.InternalContext.<>c__DisplayClasse`1.<CreateInitializationAction>b__d()
at
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action
action)
InnerException:
System.Data.Entity.Core.ProviderIncompatibleException
HResult=-2146232032
Message=This provider does not support the specified command
tree. EntityClient should be used to create a command definition from this
command tree.
Source=EntityFramework
StackTrace:
at
System.Data.Entity.Core.Common.DbProviderServices.ValidateDataSpace(DbCommandTree
commandTree)
at
System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree
commandTree, DbInterceptionContext interceptionContext)
at
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext
context, DbQueryCommandTree tree)
InnerException:
?Components used:
Entity FW 6.06.0
System.Data.SQLite 1.0.97.0
System.DataSQLite.Core 1.0.97.0
System.Data.SQLite.EF6 1.0.97.0
System.Data.SQLite.Linq 1.0.97.0
I have also tried this with 1.0.96 ver of SQLite components, but exactly same
exception is thrown in that case too.
Please let me know solution to this problem or if you need any more information.
Thanks in advance.
Regards
Ajey
--------------------------------------------------------------------------------------------------
This email and any attachments are confidential and are for the use of the
addressee only. If you are not the addressee, you must not use or disclose the
contents to any other person. Please immediately notify the sender and
delete the email. Statements and opinions expressed here may not
represent those of the company. Email correspondence is monitored by
the company. This information may be subject to export control
regulation. You are obliged to comply with such regulations.
Renishaw plc (company number 1106260) and Wotton Travel Limited (company
number 01973158) are companies registered in England and Wales with a
registered office
at New Mills, Wotton-under-Edge, Gloucestershire, GL12 8JR,
United Kingdom, Telephone +44 1453 524524.
--------------------------------------------------------------------------------------------------