How to use Entity Framework + PostgreSQL from connection? - entity-framework-4

I have already seen threads discussing the use of Entity Framework and PostgreSQL with official instructions. Those instructions need to run gacutil for every install which is not so handy for deployment purposes.
What I want to do here is passing PostgreSQL connection directly to the DbContext constructor. This is enough for me because I am going to use CodeFirst without designer. This is what I do:
public class Context : DbContext
{
Context(System.Data.Common.DbConnection connection)
: base(connection, true)
{
}
public static Context CreateContext()
{
NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=********;Database=xxx;");
conn.Open();
return new Context(conn);
}
}
But using this method I get a NotSupportedException with message:
Unable to determine the provider name for connection of type
'Npgsql.NpgsqlConnection'.
What should I do?

You'll need to register the Npgsql provider in the app/web.config. See section 3.4 Using Npgsql with ProviderFactory of the Npgsql manual.
When you install an ADO.NET provider for databases (MySQL, PostgreSQL, etc.) the installers will usually register the provider assembly in the GAC and add an entry to the machine.config. If you want to deploy without having to install the provider you'll need to include a copy of the provider assembly (set Npgsql assembly reference as Copy Local for your project) and add an entry to your application's app/web.config as follows:
<configuration>
...
<system.data>
<DbProviderFactories>
<clear />
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
...
</configuraiton>
Make sure the version matches exactly the version of the Npgsql assembly you deploy with (or just omit the Version/Culture/PublicKeyToken). The <Clear /> is there to avoid conflicts if running on a machine that already has entry for Npgsql in its machine.config. Without the clear you would get an exception. However, that's also assuming you're not relying on any other providers specified in the machine.config for your application.

Related

EF Core JET: AccessViolationException when 'updating' changed objects in MS Access

I'm building a .NET Core 3 console app with EF Core 2.2.x and EntityFrameworkCore.Jet to perform some generic reading and writing from/to an access database (mdb). I'm compiling for x86 as the docs say. Goal is to migrate lost and lots of data from a legacy system to a newer system (syncing data).
I've scaffolded the database, and I can perfectly read from the database, but as soon as I go and update an entity, I'm getting access violation exceptions:
Code sample (shortened for abbrevity):
public void SetSynced(int id)
{
var item = _db.Products.Where(x => x.Id == id).Single();
item.NeedsSyncing = false;
_db.SaveChanges();
}
When this code is hit, the following Exception shows up (no innerexception)
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I don't understand why this is happening as I can perfectly read the database, it does this only on writing.
I've tried using the following connection strings (drivers) but both with the same end-result:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\database.mdb;Jet OLEDB:Database Password = password;`
Provider=Microsoft.jet.oledb.4.0;Data Source=C:\\database.mdb;Jet OLEDB:Database Password = password;
When running on x64 reading works, but I'm getting the following exception:
System.Data.OleDb.OleDbException (0x80004002): No error message available, result code: E_NOINTERFACE(0x80004002).
I am lost and searching in the dark here. Does anyone have experience / ideas for me on where to look?
Update 02/02/2020:
I've tried manually connecting using OleDb and running the query to see where that brings me. The below code works. When I switch it out with the above code it fails again.
db = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\database.mdb;Jet OLEDB:Database Password = password");
db.Open();
var command = new OleDbCommand($"UPDATE Products SET NeedsSyncing = 0 WHERE Id= 1", db);
var res = command.ExecuteNonQuery();
After setting up JetConfiguration.ShowSqlStatements = true I got the following stacktrace:
ExecuteDbDataReader==========
UPDATE [Products] SET [PictureName] = #p0
WHERE [Id] = #p1;
SELECT ##ROWCOUNT;
#p1(Int32) = 1
#p0(String) = 'notfound.jpg'
Fatal error. 0xC0000005
at System.Data.Common.UnsafeNativeMethods+ICommandWithParameters.SetParameterInfo(IntPtr, IntPtr[], System.Data.OleDb.tagDBPARAMBINDINFO[])
at System.Data.OleDb.OleDbCommand.ApplyParameterBindings(ICommandWithParameters, System.Data.OleDb.tagDBPARAMBINDINFO[])
at System.Data.OleDb.OleDbCommand.CreateAccessor()
at System.Data.OleDb.OleDbCommand.InitializeCommand(System.Data.CommandBehavior, Boolean)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(System.Data.CommandBehavior, System.Object ByRef)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(System.Data.CommandBehavior, System.String)
at System.Data.OleDb.OleDbCommand.ExecuteReader(System.Data.CommandBehavior)
at System.Data.OleDb.OleDbCommand.ExecuteDbDataReader(System.Data.CommandBehavior)
at System.Data.Common.DbCommand.ExecuteReader(System.Data.CommandBehavior)
at System.Data.Jet.JetCommand.InternalExecuteDbDataReader(System.String, System.Data.CommandBehavior)
at System.Data.Jet.JetCommand.ExecuteDbDataReader(System.Data.CommandBehavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, Microsoft.EntityFrameworkCore.Diagnostics.DbCommandMethod, System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.Object>)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.Object>)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(Microsoft.EntityFrameworkCore.Storage.IRelationalConnection)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Microsoft.EntityFrameworkCore.DbContext, System.ValueTuple`2<System.Collections.Generic.IEnumerable`1<Microsoft.EntityFrameworkCore.Update.ModificationCommandBatch>,Microsoft.EntityFrameworkCore.Storage.IRelationalConnection>)
at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[[System.ValueTuple`2[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.ValueTuple`2<System.__Canon,System.__Canon>, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.ValueTuple`2<System.__Canon,System.__Canon>,Int32>, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.ValueTuple`2<System.__Canon,System.__Canon>,Microsoft.EntityFrameworkCore.Storage.ExecutionResult`1<Int32>>)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(System.Collections.Generic.IEnumerable`1<Microsoft.EntityFrameworkCore.Update.ModificationCommandBatch>, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(System.Collections.Generic.IReadOnlyList`1<Microsoft.EntityFrameworkCore.Update.IUpdateEntry>)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(System.Collections.Generic.IReadOnlyList`1<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry>)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
at Cashmaster.BackgroundWorker.Services.CashMasterService.Crash()
Nuget packages + versions (related to Jet):
Microsoft.EntityFrameworkCore 2.2.6
EntityFrameworkCore.Jet 2.2.0
System.Data.OleDb 4.7.0
Running this all on .NET Core 3.1.
The issue was tracked on GitHub by Migrate the x86 specific structures to AnyCPU for System.Data.OleDb #32509 and fixed with Fix x86 packing issues in System.Data.OleDb #33899 by a community member.
To fix the issue in your project, use the latest version of EF Core 3.1.x (currently 3.1.10) with the latest version of EntityFrameworkCore.Jet (currently prerelease 3.1.0-alpha.4) together with System.Data.OleDb release 5.0.0.

Using Quartz.NET with a custom connection provider

I'm trying to use Quartz.NET with ReliableDbProvider, to allow Quartz to connect to our Azure SQL databases without transient connection issues.
Here's the configuration I'm using (re-formatted for readability; I actually initialize them in a NameValueCollection...):
quartz.jobStore.dirverDelegateType: Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz
quartz.jobStore.tablePrefix: QRTZ_
quartz.dataSource.default.connectionString: Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Foo;Integrated Security=True
quartz.dataSource.default.connectionProvider.type: ReliableDbProvider.SqlAzure.SqlAzureProvider
quartz.jobStore.useProperties: true
When trying to create a scheduler, I get an exception
InvalidCastException: Unable to cast object of type ReliableDbProvider.SqlAzure.SqlAzureProvider to type Quartz.Impl.AdoJobStore.Common.IDbProvider.
I guess that's not so surprising - but how do I work around it? I took a look at the IDbProvider interface from Quartz, but it wasn't straightforward how to forward that to an instance of the ReliableDbProvider, as the latter did not implement all the features of the former.
What's the best way to use a custom connection provider that Quartz doesn't know about?
It is a QUART.NET IDbProvider....
Quartz.Impl.AdoJobStore.Common.IDbProvider
Not just the some/any standard microsoft one.
(Which you kinda make mention of, but at the same time, it doesn't look like it really clicked as to what that means)
Somebody (out there in internet land ... or you) has to WRITE a concrete that supports Azure. To my knowledge, it hasn't been done.
Here is the list:
https://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/job-stores.html
Currently following database providers are supported:
SqlServer-20 - SQL Server driver for .NET Framework 2.0
OracleODP-20 - Oracle’s Oracle Driver
OracleODPManaged-1123-40 Oracle’s managed driver for Oracle 11
OracleODPManaged-1211-40 Oracle’s managed driver for Oracle 12
MySql-50 - MySQL Connector/.NET v. 5.0 (.NET 2.0)
MySql-51 - MySQL Connector/:NET v. 5.1 (.NET 2.0)
MySql-65 - MySQL Connector/:NET v. 6.5 (.NET 2.0)
SQLite-10 - SQLite ADO.NET 2.0 Provider v. 1.0.56 (.NET 2.0)
Firebird-201 - Firebird ADO.NET 2.0 Provider v. 2.0.1 (.NET 2.0)
Firebird-210 - Firebird ADO.NET 2.0 Provider v. 2.1.0 (.NET 2.0)
Npgsql-20 - PostgreSQL Npgsql
APPEND
Hmm. I found something interesting.
In the Quartz.Net source code, in this file:
\src\Quartz\Impl\AdoJobStore\Common\dbproviders.properties
or another place to see it
https://github.com/quartznet/quartznet/blob/master/src/Quartz/Impl/AdoJobStore/Common/dbproviders.properties
Database provider configuration data
Core information taken from Spring Framework .NET - All credits to their great work!
..
# SQL SERVER
quartz.dbprovider.SqlServer-20.productName=Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0
quartz.dbprovider.SqlServer-20.assemblyName=System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
quartz.dbprovider.SqlServer-20.connectionType=System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
quartz.dbprovider.SqlServer-20.commandType=System.Data.SqlClient.SqlCommand, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
quartz.dbprovider.SqlServer-20.parameterType=System.Data.SqlClient.SqlParameter, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
quartz.dbprovider.SqlServer-20.commandBuilderType=System.Data.SqlClient.SqlCommandBuilder, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
quartz.dbprovider.SqlServer-20.parameterDbType=System.Data.SqlDbType, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
quartz.dbprovider.SqlServer-20.parameterDbTypePropertyName=SqlDbType
quartz.dbprovider.SqlServer-20.parameterNamePrefix=#
quartz.dbprovider.SqlServer-20.exceptionType=System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
quartz.dbprovider.SqlServer-20.useParameterNamePrefixInParameterCollection=true
quartz.dbprovider.SqlServer-20.bindByName=true
quartz.dbprovider.SqlServer-20.dbBinaryTypeName=Image
So it may be possible to try and do the above mappings for Azure items. No idea how close you might get.
I was expecting to see something like
public class SqlServer20 : Quartz.Impl.AdoJobStore.Common.IDbProvider
but that led me to find the "dbproviders.properties" file.

Error while running the Unit Testmethods using EntityFramework

I have an application where I am adding a new row in table using EntityFramewok. I wrote a test case which will check the functioning of this addrow method. I am getting count of rows before and after adding the row to DB.
But I am getting an exception when testmethod is trying to access the EF DB.
The specified named connection is either not found in the
configuration, not intended to be used with the EntityClient provider,
or not valid.
The error is occuring because you have a connection string in Web.Config file of your main project, but your TestProject doesnot know which DB to be referred to.
Solution:
Copy the connection string from Web.config of main project (looking similar to the one given below) and paste it into
<connectionStrings> *** </connectionStrings>
of App.config file of TestProject.
<add name="MoviesEntities" connectionString="metadata=res://*/Models.Movies.csdl|res://*/Models.Movies.ssdl|res://*/Models.Movies.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=Movies;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />

SignalR Requests Throwing "Hub could not be resolved."

I've been using SignalR since an early version and upgraded along the way however I have deployed my application to my Windows Server 2008 R2 production server and now the app crashes out with the " Hub could not be resolved." exception.
edit: StackTrace Added:
[InvalidOperationException: 'stockitems' Hub could not be resolved.]
Microsoft.AspNet.SignalR.Hubs.HubManagerExtensions.EnsureHub(IHubManager hubManager, String hubName, IPerformanceCounter[] counters) +426
Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Initialize(IDependencyResolver resolver, HostContext context) +716
Microsoft.AspNet.SignalR.Owin.CallHandler.Invoke(IDictionary`2 environment) +1075
Microsoft.AspNet.SignalR.Owin.Handlers.HubDispatcherHandler.Invoke(IDictionary`2 environment) +363
Microsoft.Owin.Host.SystemWeb.OwinCallContext.Execute() +68
Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object extraData) +414
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
Microsoft.Owin.Host.SystemWeb.CallContextAsyncResult.End(IAsyncResult result) +146
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
On my dev machine and local test server I am getting no issues.
The hub in question is really simple:
[HubName("StockItems")]
public class StockItemHub : Hub
{
}
Originally I thought it was an issue with the HubName so removed it but it still bombs out.
Originally I thought it was due to dependency injection so I then changed my Global.asax to look as follows:
var signalRResolver = new SignalRDependencyResolver();
GlobalHost.DependencyResolver = signalRResolver;
var configuration = new HubConfiguration { Resolver = signalRResolver };
RouteTable.Routes.MapHubs(configuration);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters, config.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
edit: what is SignalRDependencyResolver?
SignalRDependencyResolver didn't exist until I tried to solve this issue. As I believe its a dependency injection issue I wrapped DefaultDependencyResolver overrode GetService and GetServices to first check my Ninject kernel for the type and if not fall back to the DefaultDependencyResolver
Any ideas?
The server is running IIS7, Windows Server 2008 with .Net 4.5
The application is an MVC 4 .Net 4.5
I had this same error due to my Hub class being internal, therefore SignalR couldn't find it within my assembly.
Setting the hub to public solved the issue.
I suffer from this problem just now, and I dig in a little deeper, and have just found out a possible solution.
My hub class are not in assembly of the web project, they are in some referenced assemblies. This is a very common scenario in a multi-layer application.
When starting up, signalR will try to find hub class by an IAssemblyLocator instance. When deployed within an IIS site, this IAssemblyLocator instance find through all referenced assemblies. But at this point of time, the application is just during the startup, which means many (referenced but not loaded yet) assemblies may had NOT been gathered by owin host environment.
So the lookup for hub classes fails.
So, just add your assembly into system.web/compilation/assemblies section of Web.Config:
<system.web>
<compilation targetFramework="4.5">
<assemblies>
<add assembly="HubAssembly, Version=1.0.0.0, Culture=neutral"/>
</assemblies>
</compilation>
</system.web>
Or if you like, you can also solved this problem by implementing a custom IAssemblyLocator class, register it into the dependency resolver as soon as app.MapSignalR is invoked.
using Microsoft.AspNet.SignalR.Hubs;
public class AssemblyLocator : IAssemblyLocator {
public IList<System.Reflection.Assembly> GetAssemblies()
{
// list your hubclass assemblies here
return new List<System.Reflection.Assembly>(new []{typeof(HubAssembly.HubClass).Assembly});
}
}
// add following code to OwinStartup class's Configuration method
app.MapSignalR();
GlobalHost.DependencyResolver.Register(typeof(Microsoft.AspNet.SignalR.Hubs.IAssemblyLocator), () => new AssemblyLocator());
This is now an old question but it reared its ugly head again this weekend. After spending alot of time investigating I have found that SignalR wasn't the only thing broken in the deployment, my WebAPI was also throwing could not find controller exceptions.
Turns out this is caused by the internals of SignalR and WebApi reflecting over all the types in the Sites assembly. A TypeLoadException was being thrown , in my case due to having a class derive RoleEntryPoint which is an Azure type but as the site was being deployed in a non Azure Environment things fell apart. Simply excluding this type from non Azure builds resolved the issue.
It would be nice if these TypeLoadExceptions were more visible but there it is.
Similar to #Jijie Chen, when I hit this issue I found that it was not able to load my assembly containing my hub. The fix for me was more straightforward though. In my case I had three projects. All the logic, including the hub was in a project of its own and I had two projects intended to host using owin. One was a console project that was working fine. I then adapted that to a windows service to host it. Well, somehow I managed to forget to include a reference to the project containing my hub. This still compiled fine because the host code relies on the signalr/owin mapping functions which load the hub(s) at runtime not compile time. So when I would start up the service and try to connect I got the hub no defined error described here because it couldn't find the assembly with my hub.

"A type load exception has occurred." using DbContext with MySql connector with Mono

I am trying to use the MySQL Database with the Entity Framework Version 4.1.0.0 and Mono 2.11.4 in a ASP.NET MVC 3 Project.
On my Local Windows system everything works great but when I publish it to my Ubuntu 12.04 LTS System I get a lot of errors.
First I get:
Could not load type 'System.Data.Entity.Infrastructure.DbUpdateException' from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
when I press F5 I get different errors every time the page refreshes:
Could not load type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Could not load type 'System.Data.Entity.Infrastructure.DbCompiledModel' from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Could not load type 'System.Data.Entity.Infrastructure.ReplacementDbQueryWrapper`1[TElement]' from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Could not load type 'MySql.Data.VisualStudio.Editors.SqlEditor' from assembly 'MySql.VisualStudio, Version=6.6.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'.
Could not load type 'MySql.Data.VisualStudio.WebConfig.WebConfigDlg' from assembly 'MySql.VisualStudio, Version=6.6.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'.
A type load exception has occurred.
The last error occurred when I try to use the database with this code:
using (var db = new DefaultContext())
{
db.Persons.Add(new Person() { Name = "hallo", Address = "bllaaa" });
db.SaveChanges();
var persons = db.Persons.ToList();
return View(persons);
}
Did anybody have a similar problem and solved it? I do not know what to do.
P.S.: I use Code First Migration
From my understanding, Mono is bundling the open source version of Entity Framework, which is essentially yet-to-be-released v6.
http://weblogs.asp.net/scottgu/archive/2012/07/19/entity-framework-and-open-source.aspx
So it would seem you can't target EF v4.x, since the code has likely undergone major changes. Although, if you discover that the seemingly missing types are still there, you could attempt to work around the issue with an assembly redirect (just search for bindingRedirect).
I know why this happend.
I installed the mono-fastcgi-server4 through apt-get after i installed mono from source. So i had 2 mono versions installed and the older one was active. (2.10.8)
And the Entity Framework seems totally useless to me because only the new EF6 works with a database but there arent any provider who suppot EF6 and work with mono.

Resources