Saturday, November 13, 2010

Entity Framework Sample

You need to install sp1 before you can start with entity framework

If you can't see the framework template, do the following step
Click on Tools => Option => Projects & Solutions ..then
Click on "User item template location" browse button and set the right path
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\
do same for project templets
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\
Now click on "ok"
then close the studio and re-open again ..now you will be able to see the entity framework template

some more guide step - by step can be seen on http://www.installationwiki.org/Installing_ADO.NET_Entity_Framework

Getting problem in Connectionstring in Entity framework ?

< add name="HRRPMSContext" providerName="System.Data.EntityClient" connectionString="metadata=res://ETG.Base.Applications.EntityFramework,Culture=neutral,PublicKeyToken=null/HRRPMSContext.csdl|res://ETG.Base.Applications.EntityFramework,Culture=neutral,PublicKeyToken=null/HRRPMSContext.ssdl|res://ETG.Base.Applications.EntityFramework,Culture=neutral,PublicKeyToken=null/HRRPMSContext.msl;provider=System.Data.SqlClient;provider connection string='Data Source=ETG1\SQLEXPRESS;;Initial Catalog=ACSampleDB;User ID=HRRPMS;Password=pass;Integrated Security=True;multipleactiveresultsets=true'"/ >

In the above string ETG.Base.Applications.EntityFramework is my assembly name ETG.Base.Applications.EntityFramework.dll

Another way of creating  to connection string for entity framework is

 internal class Util
    {
        public static string EntityFrameworkConnectionString
        {
            get
            {
                SqlConnectionStringBuilder providerCs = new SqlConnectionStringBuilder();
                providerCs.InitialCatalog = CryptoService2.Decrypt(ConfigurationManager.AppSettings["DatabaseName"]);
                providerCs.UserID = CryptoService2.Decrypt(ConfigurationManager.AppSettings["UserId"]);
                providerCs.Password = CryptoService2.Decrypt(ConfigurationManager.AppSettings["Password"]);
                providerCs.DataSource = ConfigurationManager.AppSettings["ServerName"]; //CryptoService2.Decrypt(ConfigurationSettings.AppSettings["ServerName"]);
                providerCs.MultipleActiveResultSets = true;
                providerCs.TrustServerCertificate = false;
               // providerCs.IntegratedSecurity = true;
                EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
                ecsb.Provider = "System.Data.SqlClient";
                ecsb.ProviderConnectionString = providerCs.ToString();
                ecsb.Metadata = string.Format("res://{0}/HrModel.csdl|res://{0}/HrModel.ssdl|res://{0}/HrModel.msl", typeof(HRRPMSEntities).Assembly.FullName);
                return ecsb.ToString();
            }
        }


Fetching a collection using entity framework.
public static List< tbCandidate> GetAllCandidates()
{
List< tbCandidate > candidates = new List< tbCandidate>(); ;

using (var hrEntities = new HRRPMSContext())
{
foreach (var candidate in hrEntities.tbCandidate)
{
candidates.Add(candidate);
}
}

return candidates;

}

Adding data example :
public static void SetData()
{
tbCandidate candidate = new tbCandidate();
candidate.Name = "Arindam Chakraborty";
candidate.Phone = "11545645656";
candidate.Qualification = "B.Sc";
candidate.Street = "New Golden Nest";
Add(candidate);

}

public static void Add(tbCandidate obj)
{
using (var hrEntities = new HRRPMSContext())
{
hrEntities.AddTotbCandidate(obj);
hrEntities.SaveChanges();
}
}

Find a candidate by name from database
public static tbCandidate FindCndidateByName(string name)
{
tbCandidate candidate = null;

using (var hrEntities = new HRRPMSContext())
{
candidate = hrEntities.tbCandidate.FirstOrDefault(c => c.Name == name);
}

return candidate;

}

Update a candidate information

public static void Update(tbCandidate updatedCandidate)
{

using (var hrEntities = new HRRPMSContext())
{
tbCandidate candidate = hrEntities.tbCandidate.FirstOrDefault(c => c.CandidateID == updatedCandidate.CandidateID);
candidate = updatedCandidate;
hrEntities.tbCandidate.Context.ApplyPropertyChanges("tbCandidate", candidate);
hrEntities.tbCandidate.Context.SaveChanges();
}
}

Add Data in many tables with transaction scope
public static void AddMultipleRecord(tbInterview interview, tbRequirement requirement)
{

using (TransactionScope scope = new TransactionScope())
{
using (var hrEntities = new HRRPMSContext())
{

hrEntities.AddTotbInterview(interview);
hrEntities.AddTotbRequirement(requirement);

hrEntities.SaveChanges();
scope.Complete();
}
}
}


How to write join and where clause using lambada expression
using (var entities = new MyEntities())
{
var userProfile = from user in entities.UserProfile
join pro in entities.ProductInfo
on user.UserInfoID equals pro.UserInfoID
where pro.ProductInfoId == ProductId
select user;

profile = userProfile.FirstOrDefault();

}

No comments:

ETG Consultancy

Web Designing Development Analysis & Promotion
Asp.Net 2.0 SQL WWF WCF SEO Marketing Ajax JQuery NHibernate MVC