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.
Related
Microsoft.SqlServer.SqlManagementObjects 161.47027.0
Microsoft.Data.SqlClient 5.0.0
After upgrading Microsoft.Data.SqlClient from 4.1.0 to 5.0.0
I got this error creating a ServerConnection object:
using Microsoft.SqlServer.Management.Common;
var serverConnection = new ServerConnection("localhost", "sa", "mypwd");
System.MissingMethodException: 'Method not found: 'Void
Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Encrypt(Boolean)
It looks for a not implemented set_Encrypt method inside a Microsoft.Data.SqlClient.SqlConnectionStringBuilder
Does it mean Microsoft.SqlServer.SqlManagementObjects has not been updated yet to support Microsoft.Data.SqlClient 5.0.0?
If so do I need to wait for this update before I can upgrade Microsoft.Data.SqlClient 5.0.0 into my project?
You can also upgrade the ServerManagementObjects reference to 170.7.0-preview to resolve. Apparently the issue is with the smo library not the sqlclient library.
https://github.com/dotnet/SqlClient/issues/1702
Their code should be backwards compatible, but they need to be recompiled against MDS 5.0
Below is MyConnectionString"
"Server=tcp:xxx.xxxx.windows.net;Authentication=Active Directory Default;Database=TestDB;TrustServerCertificate=True;MultipleActiveResultSets=True;"
Had the error:
Method not found: 'Void Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Encrypt(Boolean)'. When usingMicrosoft.Data.SqlClient 5.0.1.`
Fixed By: Downgraded the package Microsoft.Data.SqlClient to 4.1.0.and Using Microsoft.SqlServer.SqlManagementObjects 161.47021.0.
Fixed the error. Thank you for this post.
We had the same MissingMethodException when attempting the update of Microsoft.Data.SqlClient 4.1.0 to 5.0.0 in our system. Simply updating Microsoft.Data.SqlClient - independently of Microsoft.SqlServer.SqlManagementObjects - resulted in the exception when constructing the Microsoft.SqlServer.Management.Common.ServerConnection using SQL Server Authentication (not Windows Authentication).
This snippet causes the MissingMethodException if using Microsoft.Data.SqlClient 5.0.0 (worked in 4.1.0):
Return New ServerConnection With {
.ApplicationName = My.Application.Info.Title,
.ServerInstance = connectionParameters.DataSource,
.ConnectTimeout = connectionParameters.ConnectionTimeout,
.LoginSecure = False,
.Login = connectionParameters.UserName,
.Password = connectionParameters.Password
}
For now, we are holding off on updating Microsoft.Data.SqlClient, but went ahead with the Microsoft.SqlServer.SqlManagementObjects update (161.47021.0 to 161.47027.0). I am hopeful that an upcoming Microsoft.Data.SqlClient version will fix this issue.
I am currently migrating our main NET Core 3.1 app to NET 6. Our database access layer is encapsulated into a class library using EF Core 3.1 that also needs migrating to a newer version. In that lib, there is logic to access 2 databases, Azure Sql und Oracle Sql.
The Oracle.EntityFrameworkCore nuget package is not yet available for NET6.
dotnet restore gives the following warning:
warning NU1608: Detected package version outside of dependency constraint: Oracle.EntityFrameworkCore 5.21.4 requires Microsoft.EntityFrameworkCore.Relational (>= 5.0.9 && < 6.0.0) but version Microsoft.EntityFrameworkCore.Relational 6.0.0 was resolved.
Running a query against Oracle throws a TypeLoadException
Unhandled exception. System.TypeLoadException: Method 'AppendIdentityWhereCondition' in type 'Oracle.EntityFrameworkCore.Update.Internal.OracleUpdateSqlGenerator' from assembly 'Oracle.EntityFrameworkCore, Version=5.0.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342' does not have an implementation.
at Microsoft.Extensions.DependencyInjection.OracleServiceCollectionExtensions.<>c.<AddEntityFrameworkOracle>b__0_4(ServiceCollectionMap b)
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.TryAddProviderSpecificServices(Action`1 serviceMap)
at Microsoft.Extensions.DependencyInjection.OracleServiceCollectionExtensions.AddEntityFrameworkOracle(IServiceCollection serviceCollection)
at Oracle.EntityFrameworkCore.Infrastructure.Internal.OracleOptionsExtension.ApplyServices(IServiceCollection services)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<GetOrAdd>g__BuildServiceProvider|4_0(IDbContextOptions options, ConcurrentDictionary`2 configurations)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at OracleLib.MyDbContext..ctor(DbContextOptions`1 options)
at App.Program.Main(String[] args) in C:\Users\me\app\Program.cs:line 39
at App.Program.<Main>(String[] args)
I then tried splitting the Azure Sql part and Oracle Sql part, with the latter targeting only NET5. I then dotnet pack'ed both libs and included them in an NET6 app. But again, I am getting the above warning and typeload exception.
Oracle effectively keeps me from migrating to NET6. Is there any way I can tell the oracle lib to use its own lower version of Microsoft.EntityFrameworkCore.Relational and call it a day?
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
After installing Npgsql and Simple.Data.PostgreSql from NuGet, and then using the following code:
var connectionString = ConfigurationManager.ConnectionStrings["runningJournal"].ConnectionString;
var db = Database.OpenConnection(connectionString);
var userId = db.user.Insert(userName: "foo").userId;
I get this exception:
System.TypeLoadExceptionCould not load type 'NpgsqlTypes.BitString'
from assembly 'Npgsql, Version=3.0.4.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
at Simple.Data.PostgreSql.TypeMap..cctor()
I assume that this is an issue with Simple.Data.PostgreSql since accessing the database from Npgsql works just fine. Is this a bug, or is there some other dependency that I need to manually add? Perhaps something else?
Npgsql 2.x had an NpgsqlTypes.BitString type which was removed in 3.0, which uses .NET's built-in BitArray. It would seem that the version of Simple.Data.PostgreSql which you're using is compiled against Npgsql 2.x, but somehow you're using Npgsql 3.0 in your project. Make sure you're using Npgsql 2.2.7 and not 3.0.x, and perhaps let the Simple.Data.PostgreSql project know to release a new version which works with Npgsql 3.0.
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.