Labels

slider

Recent

Navigation

Entity Framework Examples: Entity Framework Code First Examples

Entity Framework Code First Examples, entity framework code first tutorial, code first entity framework mvc, entity framework example, entity framework joins, entity framework examples in c#, entity framework tutorial

Introduction

Entity Framework is an ORM (Object Relational Mapper) which enables to developer cut off code lines. ADO.Net Entity Framework is now every C#/VB apps necessity to use in MVC, ASP.Net, and Windows applications etc. Here is explained operations with code first approach.
Main Operations (CRUD) in Entity Framework is demonstrated below with code snippet.

Pre-Requisites

If you need these snippet into your machine then require following VS 2012/2013, .Net Framework 4.5 and MS SQL Server.

Installing EF NuGet Package

Right click on Reference folder of your respective project to install Entity Framework NuGet Package and select "Manage NuGet Packages"

Installing through Package Manger Console

Alternatively, you can install Entity Framework  through Package Manger Console.
PM> Install-Package EntityFramework

Download Latest Entity Framework

Here is an official link https://www.nuget.org/packages/EntityFramework/ where you can download latest Entity Framework package.

Entity Framework Version History

Here is the complete story of all Entity Framework Versions. Top most in the list are latest versions.

EntityFramework 7.0.0-beta4
EntityFramework 6.1.3 (this version)
EntityFramework 6.1.3-beta1
EntityFramework 6.1.2
EntityFramework 6.1.2-beta2
EntityFramework 6.1.2-beta1
EntityFramework 6.1.1
EntityFramework 6.1.1
EntityFramework 6.1.0
EntityFramework 6.1.0
EntityFramework 6.1.0
EntityFramework 6.0.2
EntityFramework 6.0.2
EntityFramework 6.0.1
EntityFramework 6.0.0
EntityFramework 6.0.0-rc1
EntityFramework 6.0.0-beta1
EntityFramework 6.0.0-alpha3
EntityFramework 6.0.0-alpha2
EntityFramework 6.0.0-alpha1
EntityFramework 5.0.0
EntityFramework 5.0.0-rc
EntityFramework 5.0.0-beta2
EntityFramework 5.0.0-beta1
EntityFramework 4.3.1
EntityFramework 4.3.0
EntityFramework 4.3.0-beta1
EntityFramework 4.2.0
EntityFramework 4.1.10715
EntityFramework 4.1.10331
EntityFramework 4.1.10311

USE EntityFramework connection in ADO.Net Connection

Use Namespace

using System.Data.EntityClient

Use following code snippet to use Entity Framework connection in ADO.Net Connection

string _entityConnString = ConfigurationManager.ConnectionStrings["db_MyDatabase"].ConnectionString;
string _AdoConnString = new EntityConnectionStringBuilder(_entityConnString).ProviderConnectionString;

How to take Maximum Table ID in Database Table using Entity Framework

_context is object of Entity Framework dbContext

Entity Framework dbContext

Delclaring Object of dbContext

Model1 _context = new Model1();
Int32 _TopicIDFK = _context.myTable.Select(x => x.TableID).Max();

How to Delete Records from Database table using Enity Framework

var _item = (from c in _context.mytable.where(x=> x.id = 10) select c).FirstOrDefault();
_context.tblmyTable.Attach(_item);
_context.tblmyTable.Remove(_item);
_context.SaveChanges();

How to Save Records in Database Table using Entity Framework

var _obj = new tblmyTable();
_obj.TableID = 0;
_obj.Name = txtName.Text.Trim();
_context.tblmyTable.Add(_obj);
_context.Entry(_obj).State = EntityState.Added;
_context.SaveChanges();

How to Update/Modify Records in Database Table using Entity Framework

var _obj = new tblmyTable();
_obj = (from c in _context.tblmyTable.Where(x => x.ID == 10) select c).FirstOrDefault();
obj.ID = 10;
_obj.Name = txtName.Text.Trim();

_context.tblmyTable.Add(_obj);
_context.Entry(_obj).State = EntityState.Modified;
_context.SaveChanges();
How to Check Existing Records from database Table using Entity Framework
bool existsOption = _dtOptions.Select().ToList().Exists(row => row["Options"].ToString() == "technology");

How to run ADO.Net SQL Query using Entity Framework

string _sql = "select * from tblmyTable where ID in (" + _ids + ")";
var _img = _context.Set().SqlQuery(_sql);

How to take First Value from Database Table using Entity Framework

_IdQues = _context.tblmyTable.Select(u => u.QuestionIDPK).First();

How to count number of records using Entity Framework

_countQues = _context.tblmyTable.Where(u => u.ClassIDFK == _class && u.SubjectIDFK 
== _subject && u.ChapterIDFK == _chap && u.QuizIDFK == _quiz && u.isRecStatus == true).Count();

Rename Column Name in Database Table using Entity Framework

var query = (from c in _context.tblmyTable.Where(x => x.ID > 5).OrderBy(x => x.CountryIDPK)
 select new {Code = c.CountryIDPK, c.CountryName }).ToList();

Distinct: How to take Distinct Records from database Table using Entity Framework

var result = EFContext.TestAddresses.Select(m => m.Name).Distinct();

Pattern Matching: How to Pattern Matching using Entity Framework

var _results = from a in mycontainer.users where a.fullname.Contains(mySearchText) select a.fullname;

Join:How to join Multiple tables in Entity Framework 

I have provided here how to join multiple table using Entity Framework.

var query = (from A in _context.tblA
                 join B in _context.tblB on A.TablePk equals B.TableFK
                join C in _context.tblC on A.TablePK equals C.TableFK
                where (A.Amount >0) && (B.IsRecStatus == true) && (C.IsRecStatus == true)
                orderby A.Col1, B.Col1, C.Col1
                select new { A.tblPK, B.Col1, C.Col1, A.Col1, B.Col2, C.Col1 }).ToList();

Not In: Not In Statement using EntityFramework

Int32[] CityIDFK = {10,20,30}
var _lst = (from c in _context.tblDemo TableIDPK.Any(m => m == c.CityIDFK) select c).ToList();

Random Rows: How to pick random rows using Entity Framework

var _randomRows = (from c in _context.tblOrders.OrderBy(y => Guid.NewGuid()).Take(7) select c).ToList();

Summary

Entity Framework provides developer to code access capability so eliminate code lines. Now every .net framework (c#, Vb.net) developer is primary requirement to use Entity Framework in your application (MVC, ASP.Net, Windows applications). Major operations (CRUD) , count records, Maximum number in table, Column Rename etc of Entity Framework are explained above with coding snippet in great depth.

Suggested Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

2 comments:

  1. Replies
    1. Thanks! yes, explained in full depth about CRUD operations using Entity Framework. Hope these will help to all.

      Delete