Introduction:
Entity Framework is Microsoft's latest data access technology. Entity framework is an ORM that is used to interect with relational databases. ORM stands for Object Relational Mapper. Entity framework has few approaches to interect with database that are:
- Code First
- Model First
- Database First
Code First Approach:
Here we are going to learn Code First from scratch in console application. There are a few things you must know before working on the entity framework Code First approach. Entiry framework code classes are just classes. You can make an entity framework code class by using the normal way to add classes to your project.
Code First Conventions:
There are few conventions used by the Code First model, which means entiry framework has some rules that it follows while working with code classes. Conventions are given here.
- Tables are automatically pluralized.
- If you create a class and add a property with the same name of class and append ID at the end with this property entity framework will consider this property a “ Primary Key. ”
- Primary key of Table ( class ) built by entity framework is auto incremented by default.
Location of Database :
Entiry framework can create your database on cloud , App_Data folder of your application, intranet or internet etc. Entity framework will decide the location of your physical database upon a few factors like availability of local or online server, etc.
Data Annotations and Attributes in Code First:
Attributes are the instructions for the database table or the columns inside those tables. I’m showing you a few attributes that are commonly used in Code First to maintain database .
- Name :
Name attribute is used to explicitely set the name for table or column of table.
for example in the following table( class ) default name for the table was,- [Key]
- public int ID
- {
- get;
- set;
- }
- String Attributes
One of the important points of strings in .net is that strings are reference type which means they can be null, so entity framework shows a default behavior on reference types and set them in database columns as "nullable"( allow nulls = true ). To prevent this behavior for nullable types, use the attribute “ [ Required ]."If you do not write “ Required" with column name of datatype string, entiry framework will create a table column with nullable and varchar datatype.- [Required]
- publicstring Name
- {
- get;
- set;
- }
- Number Attributes
Numbers are value type in .net, so they need to have "values." Entity framework will not
set them as nullable. So what you'll do if you want to have a nullable number on Table is,
you’ll use the nullable datatypes in C#. see the following column. (See question mark.)- public int ? Marks
- {
- get;
- set;
- }
- Range Attributes
You can use the Range Attribute to set the range of numerical data in code
classes. And same thing you can do with strings in Code classes.- [MaxLength(30)]
- [MinLength(2)]
- public int? Marks
- {
- get;
- set;
- }
- [MaxLength(30)]
- [MinLength(10)]
- [Required]
- public string Name
- {
- get;
- set;
- }
Code First Migrations:
Its important to upgrade your database, whenever the model ( code classes ) in application changes. In order to upgrate the database, we use Database migrations in entiry framework . I’ll explain this in my upcoming article. Because here, I’m going to show you a very simple demo of code first with Console Application.
Example:
In this example we will make a database table using entiry framework code first approach and then perform CRUD operation on that table. CRUD stands for Create, Read, Update, Delete. Other database operations like building many to many and one to many relations etc, will be described later. So let's get started.







chaitanya kumarPosted Sep 6, 2018, 1:07 AM
Thank you sir
Tomas BujnaPosted May 23, 2018, 1:32 AM
Thanks for nice article. Where is located database file? I don't see anything in solution explorer.
Javaid IqbalPosted Jun 8, 2017, 8:11 AM
Excellent article please share MVC article too Thanks
Debasis SahaPosted Mar 24, 2016, 7:09 AM
Nice One..
Anil JhaPosted Mar 2, 2016, 6:15 AM
nice article
Shubham KumarPosted Feb 26, 2016, 5:47 AM
Ammar good article Plzz post article about Advance concept of entity
Vignesh ManiPosted Feb 26, 2016, 3:56 AM
Good.
Rajeev PunhaniPosted Feb 26, 2016, 1:13 AM
Nice article Ammar,Thanks for sharing.
Asfend YarPosted Feb 26, 2016, 12:19 AM
good
Raja TPosted Feb 25, 2016, 11:05 PM
Nice, Thanks for sharing