Tuesday, April 12, 2011

How to create an ADO.NET Entity Framework Model in .NET 3.5 Framework?

Here are the software/installer that you need before using ADO.NET Entity Framework:
Visual Studio 2008 Professional Edition/Team System Edition
.NET Framework 3.5
Microsoft SQL Server 2005
.NET Framework 3.5 Service Pack 1
Visual Studio 2008 Service Pack 1

Second, Create a class library, right click Add Then New Item Then look for ADO.NET Entity Data Model.
Step 1 – Right click your project. Then click add then click new item.
Step 2 – Type your chosen name for this entity model that ends with .edmx.
Step 3 – click next.
Step 4 - choose your connection or create a new connection. Just click Yes, to include connection string. If you wish to change the connection string. Just click the “New Connection…” button to change the connection. Type your chosen connection string name that will be saved in your app.config or web.config.

Step 5 – Choose the tables, views, store procedures that you wish to include in this entity model. Type your chosen name for the model namespace. Then click finish.

Step 6 – It will generate all your entity object and will display all the tables, view, stored procedures that you have chosen.

Third, create a separate Business Layer, a class and interface for each table in the tables.
A sample of simple CRUD in Linq to entity:
        ChosenEntityName dbEntity;
        Cities city;
        List<Cities> cityList;
        string connString =    ConfigurationManager.ConnectionStrings["ChosenConnectionStringName"].ConnectionString;

        public CitiesBLL()
        {
            dbEntity = new Entity(connString);
            city = new Cities();
            cityList = new List<Cities>();
        }

        public Cities RetrieveCityById(int id)
        {
            city = (from c in dbEntity.Cities
                    where c.CityID == id
                    select c).FirstOrDefault<Cities>();
            return city;
        }

        public List<Cities> RetrieveCityList()
        {
            cityList = (from c in dbEntity.Cities
                        select c).ToList<Cities>(); 
            return cityList;
        }

        public int InsertCity(Cities citys)
        {
            dbEntity.AddToCities(citys); 
            return dbEntity.SaveChanges();
        }

        public int UpdateCity(Cities citys)
        {
            city = (from c in dbEntity.Cities
                    where c.CityID == citys.CityID
                    select c).FirstOrDefault<Cities>();
            city.CityCode = citys.CityCode;
            city.CityName = citys.CityName;
            city.CountryID = citys.CountryID; 
            return dbEntity.SaveChanges();
        }

        public int DeleteCity(int id)
        {
            city = (from c in dbEntity.Cities
                    where c.CityID == id
                    select c).FirstOrDefault<Cities>();
            dbEntity.DeleteObject(city);
            return dbEntity.SaveChanges();
        }

If you are joining two or more tables, here is your linq:
            var data = (from c in dbEntity.Cities
                        join co in dbEntity.Countries
                        on c.CountryID equals co.CountryID
                        where c.CityID == id
                        select new
                        {
                            c.CityID,
                            c.CityCode,
                            c.CityName,
                            c.CountryID,
                            co.CountryName
                        });

I hope this tutorial will help you.
Here are the other references that  I used:
http://blogs.msdn.com/b/mitsu/archive/2008/03/19/how-to-implement-a-many-to-many-relationship-using-linq-to-sql-part-ii-add-remove-support.aspx
http://forums.asp.net/t/1357901.aspx
http://www.beansoftware.com/ASP.NET-Tutorials/Linq-Objects-Collections-Arrays.aspx
http://naspinski.net/post/Getting-started-with-Linq-To-Entities.aspx
http://weblogs.asp.net/salimfayad/archive/2008/07/09/linq-to-entities-join-queries.aspx
http://www.daniweb.com/software-development/csharp/threads/282950
http://stackoverflow.com/questions/2772017/linq-to-entities-left-join
http://www.c-sharpcorner.com/uploadfile/ankithakur/getting_started_with_ado_net_entity_framework09302008085544am/getting_started_with_ado_net_entity_framework.aspx
http://www.c-sharpcorner.com/UploadFile/shivprasadk/1111103052009035714AM/11111.aspx
http://www.codeguru.com/csharp/csharp/net30/article.php/c15489
http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx
http://learnentityframework.com/LearnEntityFramework/tutorials/creating-an-ado-net-entity-framework-entity-data-model/
http://www.codeproject.com/KB/linq/CRUDLINQ.aspx
http://tonesdotnetblog.wordpress.com/2008/08/15/linq-to-entities-disconnected-crud/
http://www.ibm.com/developerworks/data/library/techarticle/dm-0903linqentity/
http://www.c-sharpcorner.com/UploadFile/shivprasadk/565464607102009135202PM/5654646.aspx
http://weblogs.asp.net/bschooley/archive/2007/09/29/linq-to-sql-handling-disconnected-crud-operations-in-asp-net.aspx
http://www.dotnetspark.com/kb/667-crud-operations-using-linq-entities.aspx
http://www.stonedonkey.com/Starting-LINQ-Basic-CRUD/2008/8/40/Posts.aspx
http://www.linqdev.com/PublicPortal/publicportal/linq-to-entities.aspx

No comments:

Post a Comment