Data Block ActivationException - SQL CE Provider

Topics: Data Access Application Block
Dec 5, 2010 at 8:34 AM

I'm writing a WinForm application in C# using Visual Studio 2010 on a 64bit machine targeted for .Net 4.0.  My end goal is to build an application in which the user decides whether to use SQL CE or SQL Server as the data store.   My first step was to get the application working using SQL Server.  So I followed the hands on labs to gain access to a SQL Server version of my database.  So far so good.  Next step was to create a version of the application using SQL CE.  So I followed the procedure outlined in this article, http://msdn.microsoft.com/en-us/library/ff664387(PandP.50).aspx., for configuring the application to use SQL CE.  However it didnt' work.  When the application launches, it generates an ActivationException in the form's OnLoad event at the point where I attempt to retreive the default database. 

protected override void OnLoad(EventArgs e)
{
     String tableName = "AppDatabases";

     _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("MyTestDatabase");

     DbCommand cmd = _db.GetSqlStringCommand(String.Format("Select * from {0}", tableName));

     _db.LoadDataSet(cmd, dataSet1, tableName);

     appDatabasesBindingSource.DataSource = dataSet1;


     base.OnLoad(e);
}
I don't know what I'm missing. I've included my app.Config and the exception text below. 

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
    </configSections>

    <dataConfiguration defaultDatabase="MyTestDatabase">
        <providerMappings>
            <add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.SqlCe.SqlCeDatabase, Microsoft.Practices.EnterpriseLibrary.Data.SqlCe, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="SqlCeProvider"/>
        </providerMappings>
    </dataConfiguration>

    <connectionStrings>
        <add name="MyTestDatabase" connectionString="Data Source=|DataDirectory|\MyTestData.sdf" providerName="SqlCeProvider"/>
    </connectionStrings>

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

************** Exception Text **************
Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type Database, key "MyTestDatabase" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "MyTestDatabase".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,MyTestDatabase
 ---> System.InvalidOperationException: The type Database cannot be constructed. You must configure the container to supply this value.
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs:line 289
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs:line 71
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 110
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, NamedTypeBuildKey buildKey) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\DynamicMethodBuildPlanCreatorPolicy.cs:line 48
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 37
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 110
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 511
   --- End of inner exception stack trace ---
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 515
   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 173
   at Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceType, String key) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityServiceLocator.cs:line 64
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53
   at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 103
   at TestDataAccess03.Form1.OnLoad(EventArgs e) in C:\Dev\Tutorials\EnterpriseLibrary50\HandsOnLabs\AlsWorkingFolder\TestDataAccess03\Form1.cs:line 29
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Dec 6, 2010 at 5:52 AM

I don't know if it’s a bug or not but instead of using your own SQLCE provider (SqlCeProvider) try using the built in provider for SQLCE - "System.Data.SqlServerCe.3.5". This works from my end and hoping it will too, on you. J

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
 entlib.support@avanade.com 

 


Dec 6, 2010 at 2:23 PM

System.Data.SqlServerCe.3.5 does not appear in the Configuration Tool's Database Provider list; and the toll will not load the assembly in order to create provider mappings?

Or am i miss understanding you.  Are you suggesting not to use the Data block at all for accessing SSCE databases?

Dec 6, 2010 at 7:20 PM

I figure it out.  Something is wrong with my dev environment at home.  On my dev machine at work System.Data.SqlServerCe.3.5 does appears in the provider drop-down.  I tried it and things worked as expected.

My followup questions is where does the provider list come from: Enterprise Library install, or SQL Server CE?

Dec 7, 2010 at 12:11 AM

The list of providers are being retrieved from the machine.config, specifically under the <system.data><DbProviderFactories> section.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
 entlib.support@avanade.com