NPoco should work for Oracle database out of the box.
But when I try to create the Database object it says it cannot find the .Net Framework Data provider:
_db = new Database("connectionStringName");
I suspect there's some sort of configuration I need to do. But can't find how to do this in the documentation or any Nuget packages that might do it.
Full error message:
System.ArgumentException: System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed..
Result StackTrace:
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at NPoco.Database..ctor(String connectionStringName, Nullable`1 isolationLevel, Boolean enableAutoSelect)
at NPoco.Database..ctor(String connectionStringName, Boolean enableAutoSelect)
at NPoco.Database..ctor(String connectionStringName)
I had to install the following NuGet package: Official Oracle ODP.NET Managed Driver
Related
So I have seen the strangest behavior with the Pomelo package for .NET Core.
var optionsBuilder = new DbContextOptionsBuilder<AssessmentContext>();
var cn = String.Format(config["ConnectionStrings:AwsConnection"], config["Aws:MySQLPassword"]);
optionsBuilder.UseMySql(cn);
db = new AssessmentContext(optionsBuilder.Options);
I've had the above code in unit tests to set up connect to a MySQL database in AWS. This has worked just fine for about a week now. However, starting today, when it gets to optionsBuilder.UseMySql(cn); now I'm getting an exception.
Assessment.Tests.Data.Contexts.AssessmentContextTests.ConnectToMySQL:
Outcome: Failed
Error Message:
Class Initialization method Assessment.Tests.Data.Contexts.AssessmentContextTests.Initialize threw exception. System.TypeLoadException: System.TypeLoadException: Could not load type 'MySql.Data.MySqlClient.MySqlConnectionStringBuilder' from assembly 'MySqlConnector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d33d3e53aa5f8c92'..
Stack Trace:
at Microsoft.EntityFrameworkCore.MySqlDbContextOptionsExtensions.UseMySql(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 mySqlOptionsAction)
at Microsoft.EntityFrameworkCore.MySqlDbContextOptionsExtensions.UseMySql[TContext](DbContextOptionsBuilder`1 optionsBuilder, String connectionString, Action`1 mySqlOptionsAction)
at Assessment.Tests.Data.Contexts.AssessmentContextTests.Initialize(TestContext context) in
This has been working fine. I haven't updated any libraries, but it's failing and I'm unable to connect. Any thoughts? I'm using .NET Core 3.1, and pomelo version 3.0.1.
It's a version mismatch. MySqlConnector changed its namespace in version 1.0.0. Since this is a breaking change and Pomelo is SemVer2 compliant, Pomelo is still using MySqlConnector < 1.0.0 (currently 0.69.9).
Later versions of Pomelo (e.g. the current version 3.2.2) explicitly restrict the MySqlConnector version to below 1.0.0, so that using a MySqlConnection version >= 1.0.0 would lead to a compile time error instead of a runtime error.
So to fix the issue, use the latest version of MySqlConnector below 1.0.0.
On a side note, you should consider using EF Core 3.1.8 and Pomelo 3.2.2. There have been many bugfixes for Pomelo since version 3.0.1.
My application runs on .NET framework 4.7 and I'm using Entity Framework 6.1.3. Currently, my code uses some classes from the namespace System.Data.SqlClient such as SqlParameter. I want to switch to Microsoft.Data.SqlClient.
However, I'm not sure if EF6 is compatible with Microsoft.Data.SqlClient. This is an old article from Microsoft, it says that EF Core, EF 6 etc. haven’t yet made the transition to the new provider Microsoft.Data.SqlClient. So, I'm a bit confused.
Everything has been working well with System.Data.SqlClient for the below code
public async Task<ICollection<int>> GetChildCustomerIdsAsync(int customerId)
{
var sqlParameters = new List<SqlParameter>()
{
new SqlParameter("#CustomerId", customerId)
};
return await DbContext.Database.SqlQuery<int>("dbo.sp_GetChildCustomerIds #CustomerId=#CustomerId",
sqlParameters.ToArray()).ToListAsync().ConfigureAwait(false);
}
However, when I am switching to Microsoft.Data.SqlClient, I'm getting this error:
System.InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects.
at System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value)
at System.Data.SqlClient.SqlParameterCollection.AddRange(Array values)
at System.Data.Entity.Core.Objects.ObjectContext.CreateStoreCommand(String commandText, Object[] parameters)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternalAsync.d__6f`1.MoveNext()
No, EF 6 does not work with Microsoft.Data.SqlClient, but I have published a package that does.
NuGet package: ErikEJ.EntityFramework.SqlServer
Documentation: here and here
MS are planning it
https://github.com/dotnet/ef6/issues/823#issuecomment-948340657
We're planning for next year now, and so far this is tentatively in the plan.
https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/plan#theme-clear-path-forward-from-ef6
The exception to this is that we plan to add support for using EF6
with Microsoft.Data.SqlClient. This will be limited to runtime
support. Use of the EF6 designer in Visual Studio will still require
System.Data.SqlClient.
In the meantime, you can try adding a provider created by ErikEJ. This should help.
NuGet package: ErikEJ.EntityFramework.SqlServer
Documentation: here and here
I added a new property to my model of an ASP.NET MVC 5.2 application with MySQL database, and now I'd like to add a database migration for this by using this command:
Add-Migration RegisterPropertyAdded
But, I get an exception:
Type is not resolved for member
MySql.Data.MySqlClient.MySqlException,MySql.Data, Version=6.9.7.0,
Culture=neutral, PublicKeyToken=c5687fc88969c44d
It seems that MySQL wants to throw an exception but can't find the exception class for reasons I don't understand. I installed the following NuGet packages in the project:
EntityFramework Version 6.1.3
MySql.Data Version 6.9.7
MySql.Data.Entities, Version 6.8.3.0
MySql.Data.Entity Version 6.9.7
In the model, the class MySql.Data.MySqlClient.MySqlException is shown, so it seems like the class exists and I can't find any information that additional packages are required for the exception class.
In order to see this exception you have to update MySQL Connector/Net
Also make sure that MySQL server is up and running and you can connect to it.
For me this message appeared due to an incorrect connection string in Web.config.
I want to use SQLite with Entity Framework. I installed the SQLite and did some specific changes in my app.config file. I can see the SQLite option in the data provider list.
When I select, it asks a connection string. After I write the connection string get the error saying :
An unexpected error occured in .NET framework data provider for SQLite
Regards, Mehmet
SQLite 1.0.9xx and EF 6.1.xx are very finicky to set up. Please see if my answer here works for you (be sure to watch the video) : Database first create entity framework 6.1.1 model using system.data.sqlite 1.0.93
I have developed a new application that uses Entity Framework to access an Oracle database. This is working as expected locally, using the latest version of ODP.NET. I am now trying to deploy this application on a production server running many other legacy applications. Ideally I would like my new application to make use of its own ODP.NET / Oracle dlls and not have to change the existing Oracle install on the prod server.
I followed this guide:
http://jeremybranham.wordpress.com/2011/04/25/oracle-instant-client-with-odp-net/
Which seems to have had some success based on the comments.
However, I get the following exception when attempting to create the entities object:
Outer Exception
Exception has been thrown by the target of an invocation.
Inner Exception
The type initializer for 'Oracle.DataAccess.Client.OracleClientFactory' threw an exception.
at System.RuntimeFieldHandle.GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, Boolean& domainInitialized)
at System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck, Boolean doCheckConsistency)
at System.Reflection.RtFieldInfo.GetValue(Object obj)
at System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
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 MyAppMVC.Models.DataModels.STSProcedureEntities..ctor()
at MyAppMVC.Services.MyService.GetPersons(String lastName)
Although you appear to have resolved your problem by installing an additional client you could possible have added the following config in you app / web.config file.
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.DataAccess.Client" />
<add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET"
type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.112.3.0, Culture=neutral,
PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
And dropped the binaries in your local bin folder.
Answering my own question here. We ultimately installed a second version of Oracle, using the default settings which put the new version into a client_2 folder.
D:\oracle\product\11.2.0\client (old version)
D:\oracle\product\11.2.0\client_2 (new version)
The only additional install step was dropping in the previous TNSNAMES.ORA file into the new client_2. This file is located in client\network\admin using the file path above.
Because all of our applications were set to specifically seek out their correct Oracle versions (SpecificVersion=true) everything mostly worked without modification. For those that did not we had to drop in the older version of Oracle.DataAccess.dll (from client, not client_2) into that application's bin folder.