I need to access an AS400 db2 database from an ASP .Net 5 or Core application.
Try with entity framework through the following packages
Following the documentation on this page:
But it did not work, I presented errors of the supplier, I do not know if I was doing things correctly, if they have an example it would be great.
Ultimately I would use classic ADO, I add the used code.
project.json
"dependencies": {
"EntityFramework.IBMDataServer": "7.0.0-beta1",
. . .
},
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddViews()
.AddRazorViewEngine();
services.AddEntityFramework()
.AddDb2()
.AddDbContext<DB2.Models.Users.UserContext>();
services.AddScoped<IUserRepository, UserRepository>();
}
User.cs
using System.ComponentModel.DataAnnotations;
namespace DB2.Models.Users
{
public class User
{
[Key]
public string CODCOL { get; set; }
public string CLACOL { get; set; }
}
}
UserContext.cs
using Microsoft.Data.Entity;
using IBM.Data.Entity;
namespace DB2.Models.Users
{
public class UserContext: DbContext
{
public DbSet<User> User { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseDb2(@"Server=x;Database=x;password=x;uid=x;");
base.OnConfiguring(optionsBuilder);
}
}
}
UserRepository.cs
using System.Linq;
namespace DB2.Models.Users
{
public class UserRepository: IUserRepository
{
private UserContext _context;
public UserRepository(UserContext context)
{
_context = context;
}
public void pass()
{
var x = _context.User.First();
}
}
}
Compile perfectly, once I execute it, I see an error in the line:
System.IO.FileNotFoundException was unhandled by user code
FileName=IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208
FusionLog==== Pre-bind state information ===
LOG: DisplayName = IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208
(Fully-specified)
LOG: Appbase = file:///C:/Users/JOlivas/.dnx/runtimes/dnx-clr-win-x86.1.0.0-rc1-update1/bin/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: IBM.Data.DB2, Version= 9.7.4.4, Culture= neutral, PublicKeyToken= 7c307b91aa13d208
LOG: Fusion is hosted.Check host about this assembly.
LOG: Try host assembly store with assembly ibm.data.db2, version= 9.7.4.4, culture= neutral, publickeytoken= 7c307b91aa13d208, processorarchitecture= x86.
LOG: Try host assembly store with assembly ibm.data.db2, version= 9.7.4.4, culture= neutral, publickeytoken= 7c307b91aa13d208, processorarchitecture= msil.
LOG: Try host assembly store with assembly ibm.data.db2, version= 9.7.4.4, culture= neutral, publickeytoken= 7c307b91aa13d208.
WRN: Host assembly store does not contain this assembly.
LOG: Attempting download of new URL file:///C:/Users/JOlivas/.dnx/runtimes/dnx-clr-win-x86.1.0.0-rc1-update1/bin/IBM.Data.DB2.DLL.
LOG: Attempting download of new URL file:///C:/Users/JOlivas/.dnx/runtimes/dnx-clr-win-x86.1.0.0-rc1-update1/bin/IBM.Data.DB2/IBM.Data.DB2.DLL.
LOG: Attempting download of new URL file:///C:/Users/JOlivas/.dnx/runtimes/dnx-clr-win-x86.1.0.0-rc1-update1/bin/IBM.Data.DB2.EXE.
LOG: Attempting download of new URL file:///C:/Users/JOlivas/.dnx/runtimes/dnx-clr-win-x86.1.0.0-rc1-update1/bin/IBM.Data.DB2/IBM.Data.DB2.EXE.
HResult=-2147024894
Message=Could not load file or assembly 'IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208' or one of its dependencies.The system cannot find the file specified.
Source=EntityFramework.IBMDataServer
StackTrace:
at IBM.Data.Entity.Storage.Internal.Db2SqlConnection.CreateDbConnection()
at Microsoft.Data.Entity.Internal.LazyRef'1.get_Value()
at Microsoft.Data.Entity.Storage.RelationalConnection.Open()
at Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator'2.MoveNext()
at System.Linq.Enumerable.First[TSource](IEnumerable'1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_1'1.<CompileQuery>b__1(QueryContext qc)
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.First[TSource](IQueryable'1 source)
at DB2.Models.Users.UserRepository.pass() in D:\Proyectos\MVC\NSEL\DB2\src\DB2\Models\Users\UserRepository.cs:line 16
at DB2.Controllers.HomeController.test() in D:\Proyectos\MVC\NSEL\DB2\src\DB2\Controllers\HomeController.cs:line 33
InnerException:
(Update) Install the driver: IBM Data Server Driver Package and show me an error different from the previous one
IBM.Data.DB2.DB2Exception was unhandled by user code
ErrorCode=-2147467259
HResult=-2147467259
Message=ERROR [08001] [IBM] SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "192.168.128.5". Communication function detecting the error: "connect". Protocol specific error code(s): "10061", "*", "*". SQLSTATE=08001
Source=IBM.Data.DB2
StackTrace:
at IBM.Data.DB2.DB2ConnPool.Open(DB2Connection connection, String& szConnectionString, DB2ConnSettings& ppSettings, Object& ppConn)
at IBM.Data.DB2.DB2Connection.Open()
at Microsoft.Data.Entity.Storage.RelationalConnection.Open()
at Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator'2.MoveNext()
at System.Linq.Enumerable.First[TSource](IEnumerable'1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_1'1.<CompileQuery>b__1(QueryContext qc)
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.First[TSource](IQueryable'1 source)
at DB2.Models.Users.UserRepository.pass() in D:\Proyectos\MVC\NSEL\DB2\src\DB2\Models\Users\UserRepository.cs:line 16
at DB2.Controllers.HomeController.test() in D:\Proyectos\MVC\NSEL\DB2\src\DB2\Controllers\HomeController.cs:line 33
InnerException: