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.
Saya Software Engineer di BukaLapak.
Simak pemikian saya soal dunia Software Engineering via Twitter di @agungsetiawanmu dan facebook
Blog ini saya update seminggu sekali jadi sering-sering saja mampir
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 *