1 reply Latest Post - ‏2013-06-04T09:50:31Z by PrashanthRaoK
1 Post

Pinned topic How To: Visual Studio 2012 Entity 5 Code First DB2 9.7

‏2013-05-31T14:50:11Z |

I had a heck of a time trying to find information on this topic. I would like to save the next person down the line some hassle, so I am going to show you how to setup a simple configuration for Entity Framework Code First and Visual Studio.

Note* This is primarily for people who do read level applications. I have not tried this with inserts as I have no need for inserts or updates so this has been untested in that scenario.


First install visual studio add-ins for whatever version of DB2 you will be connecting to. This will not install anything to visual studio if you are using < 10.0 but it will install the libraries you need.

We are primarily interested in 2 libraries:

using System.Text;
using System.Threading.Tasks;
using IBM.Data.DB2.Entity;
using IBM.Data.DB2;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace YourApplication.Data
    public class DB2Context : DbContext
        public DB2Context() : base("name=DB2Entities")

        protected override void OnModelCreating(DbModelBuilder modelBuilder)


    public IEnumerable<MyModel> MyModels()
            var Query =
            SELECT *
            FROM TABLE

            return this.Database.SqlQuery<MyModel>(Query).ToList();


Then for your application settings or web settings connection string

<add name="DB2Entities" connectionString="database=MyDatabase;user id=username;password=password;server=myserver:446" providerName="IBM.Data.DB2" />

BOOM! You just connected yourself to db2.

Have an awesome day

Happy Coding.