Entity Framework Code First Custom Connection

Entity Framework Code First let you create model classes first and then generate a database and its tables based on those classes. By default it will use default database server which is localdb and default database name that follow the convention.

EF Code FIirst Custom Connection

In most cases we would rather use our own database server location and name, leaving the default one. Code First makes it easier for us to do such a thing.

Let’s say we have a connection string in Web.config like this

All we have to do is very simple. When inheriting DbContext class just make sure to pass our own string connection to base class. See code below

Hope it helps others 😉

Facebook Comments

Agung Setiawan

Agung Setiawan adalah software engineer di BukaLapak.com, penulis sekaligus pecinta sastra, dan pembaca buku

Halo, perkenalkan saya Agung Setiawan.
Seorang Software Engineer di BukaLapak, penulis buku, blogger, gunpla builder, tech educator.
Simak pemikiran saya soal dunia Software Engineering via Twitter di @agungsetiawanmu dan facebook
Mau belajar Vim bareng saya?
Belajar ngoding dari nol menggunakan PHP

Leave a Reply

Your email address will not be published. Required fields are marked *