Showing posts with label EntityFramework. Show all posts
Showing posts with label EntityFramework. Show all posts

Tuesday, April 10, 2012

How to get the primarykey name of the table in EntityFramework C#?

 Here is how to get the primary key of an entitySetName
var primaryKeyName = objDb
   .MetadataWorkspace
   .GetEntityContainer(objDb.DefaultContainerName, System.Data.Metadata.Edm.DataSpace.CSpace)
   .BaseEntitySets
   .First(meta => meta.ElementType.Name == entitySetName)
   .ElementType
   .KeyMembers
   .Select(k => k.Name)
   .FirstOrDefault();

How to check the Key exists or not in EntityFramework C#?

This method will help to get the key exists or not from an entityset in EntityFramework
Here
TargetTable is the Enum where I have all the tables names
string keyValue is the value for which i am checking exists or not

 public bool CheckKey(TargetTable tableName, string keyValue, MyMotorEntities objDb)
{
bool isKeyAlreadyExists = false;
try
                            {
                                string entitySetName = Enum.GetName(typeof(TargetTable), tableName);
                                var primaryKeyName = objDb
   .MetadataWorkspace
   .GetEntityContainer(objDb.DefaultContainerName, System.Data.Metadata.Edm.DataSpace.CSpace)
   .BaseEntitySets
   .First(meta => meta.ElementType.Name == entitySetName)
   .ElementType
   .KeyMembers
   .Select(k => k.Name)
   .FirstOrDefault();
                                List<KeyValuePair<string, object>> lstEnum = new List<KeyValuePair<string, object>>();
                                lstEnum.Add(new KeyValuePair<string, object>(primaryKeyName, keyValue));
                                var lsttype = from t in lstEnum select t;
                                IEnumerable<KeyValuePair<string, object>> IeList = ((IEnumerable<KeyValuePair<string, object>>)lsttype).Select(x => x).AsQueryable();
                                System.Data.EntityKey key = new System.Data.EntityKey("MyMotorEntities." + entitySetName , IeList);
                                var tbl = objDb.GetObjectByKey(key);
if (tbl!=null)
                {
                    isKeyAlreadyExists = true;
                }
                            }
                            catch (Exception ex)
                            {

                            }
 return isKeyAlreadyExists;
}

Thursday, February 2, 2012

How to update a record through EntityFramework in C#?

To update record with the help of EF we use this code
public void Update(tblAdmin objtblAdmin)
{
try
{
using (MyMotorEntities objDb = new MyMotorEntities())

{
var objAdmin = (from t in objDb.tblAdmins.Where(p => p.IdAdmins == objtblAdmin.IdAdmins) select t).FirstOrDefault();
if (objAdmin.IdAdmins > 0)
{objAdmin.Email = objtblAdmin.Email;
objAdmin.Firstname = objtblAdmin.Firstname;
objDb.SaveChanges();

}

}

ReportSuccess();

}
catch (Exception ex)
{
ReportError(ex);
}
}

But what happens when the tblAdmin has lots of fields :) ok don't worry we can use Attach for this.Attach allows us to attach to a record within the context, perform a straight replace on the record with the new modified record and save back the changes and All just within just 3 lines of codes... lolz :)

Here is this ...

public void Update(tblAdmin objtblAdmin)

{
try
{

using (MyMotorEntities objDb = new MyMotorEntities())
{
objDb.tblAdmins.Attach(objDb.tblAdmins.Single(c => c.IdAdmins == objtblAdmin.IdAdmins));
objDb.tblAdmins.ApplyCurrentValues(objtblAdmin);
objDb.SaveChanges();
}

ReportSuccess();
}
catch (Exception ex)
{
ReportError(ex);
}
}

Cheers :)