Exception thrown when creating sqlite .net entity data model - linq

I'm trying to get sqlite working with linq. So far I've:
installed the ADO.net providers from http://sourceforge.net/projects/sqlite-dotnet2/files/SQLite%20for%20ADO.NET%202.0/
installed both the 32 and 64 bit sqlite .net from http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
Now I can create an entity data model using an sqlite database as the source. Whenever I try to create a new instance of the data model (in a wpf component that is getting displayed) I get the following exception:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type myprogram.mycomponent' that matches the specified binding constraints threw an exception.' Line number '13' and line position '37'.
The mycomponent constructor is simply:
private void myprogram()
{
InitializeComponent();
pricesEntities pe = new pricesEntities();
}
When I open the design view for the window that displays the component, I get an
ArgumentException was thrown on "mycomponent": Cannot create an instance of "mycomponent".
An Unhandled Exception has occurred
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at myprogram.pricesEntities..ctor() in <path>\prices.Designer.cs:line 34
at myprogram.ItemList.InitializePriceList() in <path>\ItemList.xaml.cs:line 34
at myprogram.ItemList..ctor() in <path>\ItemList.xaml.cs:line 29
My app.config contains the following connection string:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="pricesEntities" connectionString="metadata=res://*/prices.csdl|res://*/prices.ssdl|res://*/prices.msl;provider=System.Data.SQLite;provider connection string='data source="<path>\prices.db"'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
I tried copying the app.config file to the directory of the executable, as was suggested here and other places. That did not help. Note that this is a different issue than The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
I have only one project (a wpf application) that I am trying to get to work.

Figured out it was because it was an assembly build for the 2.x runtime, so I added the useLegacyV2RuntimeActivationPolicy element to my app.config and that resolved the issue.

Related

How to use Oracle Entity Framework with no config file

Is it possible to create a code-first Entity Framework model that connects to an existing database using ODP.Net without having any settings in the app.config file?
I have tried many different things.
Currently I am setting DbConfiguration:
sealed class EntityFrameworkConfiguration : DbConfiguration
{
public static readonly DbConfiguration Instance = new EntityFrameworkConfiguration();
EntityFrameworkConfiguration()
{
this.SetDefaultConnectionFactory(new OracleConnectionFactory());
this.SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance);
}
}
DbConfiguration.SetConfiguration(EntityFrameworkConfiguration.Instance);
I am passing an OracleConnection directly into the EF context.
However, I either have problems with the SQL being generated in SQL Server format (using double-quotes around table aliases), or I get the following error:
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
Additional information: Unable to determine the provider name for provider factory of type 'Oracle.ManagedDataAccess.Client.OracleClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
Has anyone any experience of getting this to work without polluting app.config with crud?
Yes. To complete the switch from machine.config/app.config to code-based configuration, I had to also include a call to SetProviderFactory().
public sealed class EntityFrameworkConfiguration : DbConfiguration
{
public static readonly DbConfiguration Instance = new EntityFrameworkConfiguration();
public EntityFrameworkConfiguration()
{
SetDefaultConnectionFactory(new OracleConnectionFactory());
SetProviderServices("Oracle.ManagedDataAccess.Client", EFOracleProviderServices.Instance);
SetProviderFactory("Oracle.ManagedDataAccess.Client", new OracleClientFactory());
}
}
I also called DbConfiguration.SetConfiguration(EntityFrameworkConfiguration.Instance); in the startup of my application because I had DbContext's in multiple assemblies that all needed to share this configuration.
On a side note, I have also found this to be effective in allowing your application to work around the ConfigurationErrorsException: The 'DbProviderFactories' section can only appear once per config file for cases where you may not have access to repair the user's machine.config.
Uff. Found the problem.
Because I was registering column mapping using lower case the query didn't work. The column and table names must be in upper-case.
How silly.

ConnectionString Exception After Installing WebService

I have a webservice that I am using to get information from users. Before installing the service, it works fine. After installing the service, I am getting an exception "Object reference not set to an instance of an object." The WCF is referencing a dll where validating and writing to the database and return the results. The dll library has an app config where I added the connection string
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<clear/>
<add name="reqInformation" connectionString="Data Source=databasesourceInitial Catalog=databasename;Persist Security Info=True;User ID=username;password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
I have this class that is getting the connection string
public abstract class BASEClass
{
public static string GetConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["reqInformation"].ConnectionString;
}
}
}
I added the same connection string to the wcf config file.
In addition, I added a test project to test the service before installing it, it worked fine. I installed the WCF Service and ran the test project again, and it failed. The exception was Object reference not set to an instance of an object.
What am I doing wrong here?
The app config was added to the service Library; however, the service library was not the executing assembly. As a result, it was giving that exception. I moved the connection string int he executing assembly, recompile the code, re-install the service, and test it. All things are working as expected.

EF 4.3 config section

Hi I have the following in my web.config
<entityFramework>
<contexts>
<context type=" Dashboard.Domain.DataContext, Dashboard.Domain">
<databaseInitializer type="Dashboard.Domain.DataContextInitializer, Dashboard.Domain" />
</context>
</contexts>
the problem is that when I now try to scaffold via the gui tools in vs 2010 it throws a design time exception
Unable to retrieve meta data for 'entity type' Failed to set database initlizer of type 'Init type goes here' specified in app config. see inner exception
why does this happen it also happened with the EF 4.1 way of defining the intializer in config.
Thanks

MVC 3 Scaffolding Template creation error

I'm trying to generate a Controller usind the Scaffolding Template "Controller with read/write actions and views, using Entity Framework".
I created an ADO.net Data Entity Model and connected it to an MSSQL 2008 DB (hosted by GoDaddy) and I am able to select the correct Model class and Data context class, but when I click "Add", I get the following error:
Unable to retrieve metadata for 'Project.Models.ModelClass'. The entry 'ProjectDataContext' has already been added. (C:\Users...\Temp\tmpD167.tmp line 13)
The file "tmpD167.tmp" is a copy of my web.config
Line 13 is a connection string that looks like this (post sanitation):
<add name="ProjectDataContext" connectionString="metadata=res://*/Models.Project.csdl|res://*/Models.Project.ssdl|res://*/Models.Project.msl;provider=System.Data.SqlClient;provider connection string="data source=project.db.12345678.hostedresource.com;initial catalog=projectdb;persist security info=True;user id=projectdbuser;password=Password00##;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I have 2 other connection strings (one for ApplicationServices and the other for the project data) that are on lines 11 and 12.
Is my connection string formatted correctly?
can you remove your .edmx file and the reference to it in the web config and thenrecreate it?

System.ComponentModel.DataAnnotations MethodAccessException in Automated Controller Unit Test, MVC

First of all I'd like to say that I wouldn't doubt if this is a configuration error, because it's only happening on our CI build server and not on anyone's local development machine. So if anyone has a seemingly obvious configuration suggestion please feel free to point it out, my team doesn't configure the build server so there could be something simply not set up correctly.
I'm running into an issue with one of my automated tests, I'm having this issue in any test that involves the (System.ComponentModel.)DataAnnotations on the Entity that is being used. It seems to only occur with any test that involves the Controller calling TryUpdateModel.
Before I provide the entire error message, here’s a list of some of the things we’ve already tried:
Adding the main MVC assemblies to the “fullTrustAssemblies” section of the app.config of the Test project
Adding the System.ComponentModel.DataAnnotations assembly to the “partialTrustVisibleAssemblies” section of the app.config of the Test project
Adding the System.ComponentModel.DataAnnotations assembly to the AppDomainSetup PartialTrustVisibleAssemblies in code
Trying to use ReflectionPermission.Demand to see if there was a problem with permissions
Ensuring that .NET Framework 4 and MVC3 were installed on the machine
Checking for the registry key(s) talked about in this article
Here's the full error:
Test method qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState threw exception:
System.MethodAccessException: Attempt by security transparent method 'System.Web.Mvc.TypeDescriptorHelper.Get(System.Type)' to access security critical method 'System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider..ctor(System.Type)' failed.
Assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is a conditionally APTCA assembly which is not enabled in the current AppDomain. To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'System.ComponentModel.DataAnnotations, PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain
Test method qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState threw exception:
System.MethodAccessException: Attempt by security transparent method 'System.Web.Mvc.TypeDescriptorHelper.Get(System.Type)' to access security critical method 'System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider..ctor(System.Type)' failed.
Assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is a conditionally APTCA assembly which is not enabled in the current AppDomain. To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'System.ComponentModel.DataAnnotations, PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain.<br> at System.Web.Mvc.TypeDescriptorHelper.Get(Type type)
at System.Web.Mvc.ModelBinders.GetBinderFromAttributes(Type type, Func`1 errorMessageAccessor)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, IModelBinder fallbackBinder)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, Boolean fallbackToDefault)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at qTrade.UI.Web.Controllers.Maintenance.TypeLookupController.Create(FormCollection collection) in d:\cibuild\qTrade\qTradeCI\Sources\source\UI\qTrade.UI.Web\Controllers\Maintenance\TypeLookupController.cs:line 55
at qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState() in d:\cibuild\qTrade\qTradeCI\Sources\source\Tests\qTrade.UnitTests\Web\Controllers\Maintenance\TypeLookupControllerTest.cs:line 178
at System.Web.Mvc.TypeDescriptorHelper.Get(Type type)
at System.Web.Mvc.ModelBinders.GetBinderFromAttributes(Type type, Func`1 errorMessageAccessor)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, IModelBinder fallbackBinder)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, Boolean fallbackToDefault)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at qTrade.UI.Web.Controllers.Maintenance.TypeLookupController.Create(FormCollection collection) in d:\cibuild\qTrade\qTradeCI\Sources\source\UI\qTrade.UI.Web\Controllers\Maintenance\TypeLookupController.cs:line 55
at qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState() in d:\cibuild\qTrade\qTradeCI\Sources\source\Tests\qTrade.UnitTests\Web\Controllers\Maintenance\TypeLookupControllerTest.cs:line 178
Any suggestions would be greatly appreciated, thanks!
I Fixed the problem !
This is a combination of running in Medium Trust, having MVC installed in the GAC and using "dynamic" (or a shape) as a ViewModel. Only (and only if) these 3 conditions are true you run into this error. The solution is to change your controller code: instead of returning "View(model)", return "View((object)model)".

Resources