Calling AS400 DB from .Net Core - db2

I have a .Net Core WebAPI, and I have the IBM.Data.DB2.Core package installed.
When I try to call the DB2Connection constructor with a connection string, I'm getting the following error.
Unable to load DLL 'db2app64.dll': The specified module could not be found.
Is this something that should be included with the DB2.Core package? Is there a better way to call an AS400 DB from .Net Core?
My code looks like this:
DB2Connection conn = new DB2Connection("Server=as400.example.com;Database=CLIENTS01;UID=user1;PWD=password1;");

If you are using the .NET Core package, you will have to include the path to the driver (which is included in the NuGet package) in your PATH variable.
Here's the relevant part of the FAQ on the NuGet package on Developer Works:
Q: Do I need to do any additional configuration for using this
configuration.
Yes, On Windows :
if C:\Users\<USERNAME>\ is the NuGet package folder then
add to the path: C:\Users\<USERNAME>\.nuget\packages\IBM.Data.DB2.Core\1.0.0.100\build\clidriver\bin
and On Linux:
append $HOME/.nuget/packages/IBM.Data.DB2.Core-lnx/1.0.0.100/build/clidriver/lib
to the LD_LIBRARY_PATH.
I've found the easiest way to get it set is to set the PATH variable in your project settings. Check out this blog post about some different ways you can do that.
In fact, this other Developer Works page says:
Instructions for downloading and using the package
The following are the prerequisites for using the package
Any other IBM DB Drivers should not be present in the machine.
Path/LD_LIBRARY_PATH needs to be updated to include the package driver path.

you need to install the IBM Data Server Driver Package wich you can get over there
http://www-01.ibm.com/support/docview.wss?uid=swg24038920
it includes the dependencies you are missing

Related

How to write a NuGet package that updates the binding redirects when the package reference is upgraded?

We use VS 2017 and consume NuGet packages the old way. We do not use PackageReference as of yet.
When a NuGet package reference is updated through the NuGet Manager in VS, the respective assembly binding redirect is not updated automatically - we have to do it manually.
So, I suppose it is up to the package to take care of it through the tools\install.ps1 script. Now, I think I know how to implement it, but I do not want to invent the wheel. Surely the code already exists somewhere, but where?
Clarification
Our application is strongly signed and it targets .NET 4.5.2 currently (soon to be upgraded to 4.7.2). We use packages.config.
I need to explain what is going on. There are three players on the field:
A tool - DbUpgrade
The tool plugin Api - DbUpgradeApi
An implementation of the plugin Api - LogDbUpgradeProgress
Let us inspect the DbUpgradeApi package. 3 versions of it are relevant to us:
The version against which LogDbUpgradeProgress is compiled - A
The version against which DbUpgrade is compiled - B
The latest version of DbUpgradeApi - C
The DbUpgrade tool loads the plugin LogDbUpgradeProgress at runtime. The binding redirects are needed, because A is not the same as B (and because the code is signed, nothing to do here currently)
If C is higher than B, then we should update the reference to DbUpgradeApi in DbUpgrade. But doing so must be accompanied with updating the binding redirect. And this is the essence of this question.
Ok, so I just spent the last hour testing, and I didn't need to do anything that I consider special for binding redirects to work.
But first, are you sure you need binding redirects? .NET Core doesn't need it. If you're using .NET Framework, but with a project using PackageReference, then it's resolved at build time, your app.config doesn't need the binding redirect in the version that's checked in with your code, but when you build and check the [your exe name].config file it does have the binding redirects. Also, binding redirects only matter when your assembly has strong naming. If you didn't sign your assembly, then binding redirect isn't needed.
Here are the steps that I took to create a reproduction of getting binding redirects in a console app using packages.config.
Create an empty folder to start with. I used dotnet new sln, dotnet net nugetconfig to generate a new sln and nuget.config file. I edited the nuget.config file to add the folder localFeed as a source, and set the globalPackagesFolder to gpf so I didn't pollute my real global packages folder with test packages. Also created a strong name key with sn -k snk.snk.
Create first test classlib. dotnet new classlib -n someLib. I edited Class1.cs to change the class name to SomeClass and added a property that retusns the value "Version 1". Used Visual Studio to set snk.snk as the signing key. dotnet pack to generate V1 of the package. I then edited SomeClass to change the message to "Version 2" and then ran dotnet pack /p:version=2.0.0. Finally, used nuget.exe add -source localFeed someLib\bin\Debug\someLib.1.0.0.nupkg and again for v2 of the nupkg.
Create the second test classlib, dotnet new classlib -n anotherLib and set the signing key to snk.snk. Changed Class1.cs to AnotherClass and added a property public string Message => new someLib.SomeClass().Message;. Added a reference to someLib version 1 in the csproj, then built, packed and used nuget.exe to add the nupkg to localFeed.
Opened Visual Studio and created a .NET Framework console app. Added a reference to anotherLib, which automatically brought in v1 of someLib. Upgraded the reference of someLib to v2, and confirmed that packages.config now has a binding redirect for someLib.
Created another .NET Framework console app and did the same as step 3, but this time using PackageReference instead of packages.config. The project app.config doesn't have binding redirects, but the .config file in the bin folder after build does.
edit: perhaps an important part to understanding NuGet/MSBuild binding redirect behaviour is the following: In both steps 3 and 4, if I add a reference only to anotherLib, then no binding redirect is created because all assembles that reference someLib reference the same version. Only by making my console app reference a different version of someLib than anotherLib uses, then the binding redirect is created.
In an app with plugins, the building the app assembly won't have a binding redirect, because it's the only assembly in the compile command line that uses the plugin contract dll, so no conflict to need a binding redirect. When the plugin assembly is built, only the plugin depends on the plugin contract dll, so again no conflict so no binding redirect. If you copy all the dlls into a single folder, then there can be a conflict in the required version, but this is a deployment time issue, not a build/compile time issue, so build tools may not help. One way to resolve this would be to add a reference to the plugin project from the app assembly. This way at compile time the build tools can see that two different versions of the plugin contract dll is used, so a binding redirect can be created. This only works if you build the app assembly. If the app is just a binary that you're given, then changing the binding redirects becomes a deployment time responsibility, so development/build tools may not help.

UseMySQL extension method not recognized in ASP.NET core

Environment: Ubuntu 16.04, .NET Core 1.10 (1.0.0-preview2-1-003177), Visual Studio Code 1.8.1
I have created an ASP.NET MVC Core application by running the following command:
$ dotnet new -t web
I am able to load the folder in VSC and debug it.
By default, the engine generates code for Sqlite. I am changing it to use MySQL. My changes are based on the information from the following two articles:
http://insidemysql.com/howto-starting-with-mysql-ef-core-provider-and-connectornet-7-0-4/
https://damienbod.com/2016/08/26/asp-net-core-1-0-with-mysql-and-entity-framework-core/
First, I added the following lines into dependencies section of project.json.
"MySql.Data.Core" :"7.0.4-ir-191",
"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"
After running dotnet restore, the required DLLs were downloaded.
The next step was to modify Startup.cs and modify
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
to
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
Essentially, I am replacing UseSqlite by UseMySQL.
However, extension method UseMySQL or UseMySQL do not seem to be available on DbContextOptionsBuilder.
Wondering if I missed some step somewhere. Regards.
Add the using MySql.Data.EntityFrameworkCore.Extensions; statement.
I've successfully used this in my Mac with the latest version of VS Community in to an ASP CORE MVC project and I just needed to add the following NuGet-Packages:
Microsoft.EntityFrameworkCore
MySql.Data.EntityFrameworkCore
Then using them on your class like:
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
Notice the MySQL on the USING statement is CAPITAL letter, and it is MySql on the NuGet Package name.
Hope this hel
I also had the same issue even though was installing the Pomelo.EntityFrameworkCore.MySql,
just installed the package through the terminal:
dotnet add package Pomelo.EntityFrameworkCore.MySql
it worked!

How can I run an Entity Framework 7 migration on a remote database using Powershell?

In a standalone entity framework 7 project (note, not a MVC project with an entity context where the command DNX might be used), how can I run a migration on a remote database using Powershell?
I am currently using Entity Framework 7.0.0-rc1-final with Visual Studio 2015 (14.0.23107.0).
At the moment there is no way you can use pure PowerShell to do this because a utility like migrate.exe does not exist yet and importing the EF PS modules is not possible as they require a Package Manager PowerShell Host.
Here are some ideas how you can update a remote db in EF7:
One thing you could do is use the package manager console commands from within VS as usual to update the remote db. You can create a second context that has the remote db connection string and use the update-database command specifying the context to use. These commands require the following package in EF7:
https://www.nuget.org/packages/EntityFramework.Commands/.
I have done this successfully in a class lib project.
Another solution would be to use DNX commands by creating a DNX project instead of a classic one. DNX projects are not just for web sites, it is just another type of project. Here is a link that shows how to create a console app DNX project:
http://docs.asp.net/en/latest/dnx/console.html.
So with this type of project you can use the provided DNX commands that you seem to be aware of.
I hope this helped. Maybe we can give more help if you describe your situation and your end goal in more detail.
Answer too long as a comment, so adding it here...
Have you looked at this article and the links in the answer?
From that answer
The problem with importing the module into a PowerShell console is that I believe the module expects to run in a context where it has a Visual Studio DTE object available. That environment is the NuGet Package Manager Console. This issue has been brought up before. Check out this blog post and this SO question.
This blog post shows how to write code that does migrations.
What might be helpful for readers of this question is what you have tried, what is not working, and other information that might help solve your problem.

trouble with service reference

I added a service reference that points to http://api.microsofttranslator.com/V1/soap.svc in my project. After adding the reference an app.config file was created.
When I run the project everything works. After creating a setup file with Advanced Installer (program for authoring Windows Installer setup packages) I get the following error message when I run the installed program.
"Could not find default endpoint element that references contract TranslatorService.LanguageService in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."
Can someone help?
This problem is not generated by the setup package created with Advanced Installer, it seems to be .NET specific. Here is another similar thread: Microsoft translator error

Is there any class similar to ProvidersHelper but not for web?

Is there any class similar to ProvidersHelper but not for web?
I want to instantiate a collection of providers. Actually I'm using CodeSmith & Nettiers, and the db provider is Oracle. It generated a provider section to be added at app.config, and i dont know why it uses System.Web.Configuration classes or why at this section it says SQLClient instead of OracleClient.
The error that I'm getting is:
"An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Web.dll
Additional information: Could not load file or assembly 'LLPA.Data.OracleClient' or one of its dependencies. The system cannot find the file specified."
Any idea or solution is appreciated.
It is probably a bug if it refers to SQL Server and you are using Oracle. I'd recommend logging a bug on the .netTiers issue tracker. I'd recommend taking a look at all of the LLPA.Data.OracleClient project references are being built and ensure that they are in your bin directory of your compiled application.
Check the DLL LLPA.Data.OracleClient is in the bin folder or the GAC. If that doesn't work look into using Fuslogvw.exe.
I hadn't had added the reference of LLPA.Data.OracleClient to my application, that's why it couldn't find it.