Question

I need to connect my ASP Net 5 application with a IBM Db2 database.

I found these nuget package:

https://www.nuget.org/profiles/IBMDB2EF

And i followed this tutorial:

https://www.ibm.com/developerworks/community/blogs/96960515-2ea1-4391-8170-b0515d08e4da/entry/sample_ef7_application_for_ibm_data_servers?lang=en

But that does not work.

My project.json

"dependencies": {
"EntityFramework.IBMDataServer": "7.0.0-beta1",
. . .

}

My Starup.cs

public void ConfigureServices(IServiceCollection services) { . . .

        services.AddEntityFramework()
            .AddDb2()
            .AddDbContext<DB2.Models.Users.UserContext>();

        services.AddScoped<IUserRepository, UserRepository>();
    }

My DBContext

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.x.x.x;Database=x;password=x;uid=x;");
            base.OnConfiguring(optionsBuilder);
        }        
    }
}  

My repository

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();            
        }
    }
}

When execute this line "var x = _context.User.First();" show me this error:

System.BadImageFormatException 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.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

    HResult=-2147024885
    Message=Could not load file or assembly 'IBM.Data.DB2, Version=9.7.4.4, Culture=neutral, PublicKeyToken=7c307b91aa13d208' or one of its dependencies. An attempt was made to load a program with an incorrect format.
    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: 
1
0
4/4/2016 8:36:46 PM

Popular Answer

I see you marked db2400 - is this an IBM i or iSeries? If so, it has its own version of DB2. If the above is true use what comes with IBM i Access for Windows and add a reference to the following:

C:\Program Files (x86)\IBM\Client Access\IBM.Data.DB2.iSeries.dll

Your installation could be in a different directory. This will give you what you need to access.

1
4/1/2016 9:44:23 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow