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.
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
1 2 3 |
<connectionStrings> <add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" /> </connectionStrings> |
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
1 2 3 4 5 6 |
public class MyContext : DbContext { public MyContext : base("myConnectionString") { } } |
Hope it helps others 😉
Facebook Comments