entity framework is a great tool for doing Database manipulations of your Application. All the Time we need to use Database for managing data. the same job we have to do over and over again. But Entity Framework makes the job easier by writing all the boilerplate codes for you. There are 3 types of Entity Framework
- Code First
- Model First
- Database First
#Code First Approach Code First Mainly useful for Domain Driven Design
- Write Application Domain & Context Classexs
- Configure Domain Classes For Additional Mapping Requirements
- Build Application
- Code First API to create a new database or map existing Database with Domain Classes
- Seed Default/Test Data into the Database
- finally Launch the Application
base constructor of the context class can have the following parameters
- No Parameter
- Database Name
- Connection String Name
if you donot specify the above figure base constructor of the context class then it creates a database in your Local SQLExpress Server with a name that matches your {Namespace}.{Context Class Name}
For Example Code First will create a database named SchoolDataLayer.Context
for the following context class
namespace SchoolDataLayer { public class Context : DbContext { public Context():base(){} } }
You can also specify the database name as a parameter in a base constructor of the context class. If you specify a database name parameter, then Code First creates a database with the name you specified in the base constructor in the local SQLExpress database Server.
For Example Code FIrst will create a database Named MySchoolDB for the following Context Class
namespace SchoolDataLayer { public class Context:DbContext { public Context():base("MySchoolDB"){} } }
you can also define connection string in app.config or web.config and specify connection string name starting with the base constructor of the context class
Consider the following example where we pass name = SchoolDBConnectionString
parameter in the base constructor
namespace SchoolDataLayer { public class Context: DbContext { public SchoolDBContext():base("name=SchooLDBConnectionString"){} } }
`
`Thus Code-First use the base constructor
parameter to initialize a database