Entity Framework – CodeFirst Approach

Assalamualaykum Wr Br..:)
Today we are going to discuss Entity Framework Code First approach with a simple console application demonstrating  School activities with two classes named as Student & Standard.
As we know student have attributes like StudentId, Name, Standard, Age & Date Of Birth hence we are going to create properties with same name in class Student.
student
Similarly, Standard class do have properties like StandardId, StandardName but this class can have multiple students hence we are using ICollections of type Student as shown below:
standard
Now I would create a new class with SchoolDBContext name where DBContext is mandatory for flexible default operations with built in libraries of Entity Framework. This DBContext have dataset hence its return type would be Dbset and its properties are classes with s appended atlast with name as shown below:
schoolcontext
Finally it is required to call the DBContext class in main method as shown below:
program
With this Execution of program, Entity Framework creates the New database with tables as designed with classes as shown below:
dbcontext

With this it is observed that based on the code design DB is being generated hence it is regarded as Code First Approach.

Thats it for today..:Jazakallah khair

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s