Click here to Skip to main content
16,013,548 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In my application I have a method which is used to check if the record is already exist or not for Insert and Update purpose for a tablename and keyvalues.
For this i used this code
C#
public bool CheckKey(string sqlQuery, MyMotorEntities objDb)
        {
            bool isKeyAlreadyExists = false;
            try
            {
                if (objDb == null)
                {
                    objDb = new MyMotorEntities();
                }

                ObjectQuery<DbDataRecord> query = new ObjectQuery<DbDataRecord>(sqlQuery, objDb, MergeOption.NoTracking);
                if (query.Count() > 0)
                {
                    isKeyAlreadyExists = true;
                }

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

            return isKeyAlreadyExists;
        }


Another methode is bellow

C#
public bool CheckKey(string tableName, string keyName, string keyValue, MyMotorEntities objDb)
        {
            bool isKeyAlreadyExists = false;
            try
            {
                if (objDb == null)
                {
                    objDb = new MyMotorEntities();
                }

                string commandText = @"SELECT VALUE t." + keyName + " FROM MyMotorEntities." + tableName + "s AS t WHERE t." + keyName + "==@keyValue";
                ObjectQuery<DbDataRecord> query = new ObjectQuery<DbDataRecord>(commandText, objDb, MergeOption.NoTracking);
                query.Parameters.Add(new ObjectParameter("keyValue", Convert.ToInt32(keyValue)));

                if (query.Count() > 0)
                {
                    isKeyAlreadyExists = true;
                }

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

            return isKeyAlreadyExists;
        }


And I did with a switch statement for tablename too with filtering keyvalues.
But is there any other good option where we can pass the entity object(table) and for that it will return the result as exists or not.

We have to check all tables records before insert,update or delete and I want a single methode will do that without using switch statement and passing inline query.
Please suggest.
Posted
Updated 9-Apr-12 1:25am
v3

Some how I manage to solve my problem


XML
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();



System.Data.EntityKey key = new System.Data.EntityKey(DefaultContainerName + "." entitySetName , KeyName,KeyValue);
//DefaultContainerName =objDb.DefaultContainerName

var tbl = objDb.GetObjectByKey(key);

tbl is the record of that entityset for the key and the related value
 
Share this answer
 
v2
Don't make it more completed than it is
Entity obj = context.entity.FirstOrDefault(e => e.key == key);
if(obj == null)
  // Does not exist
else
  // Exists
 
Share this answer
 
Comments
Bishnu Tewary 9-Apr-12 7:37am    
Hi Mark,
Thanks for you reply but for this methode
public bool CheckKey(string tableName, string keyName, string keyValue, MyMotorEntities objDb)

Can you please describe which is the key?
because here I am passing the table name so key will be differ for tables.
I want to create a methode which takes Entity object and that object will be checked from the db and returs exist or not.

I am not using Enityframewrok Code First Here its a Enityframewrok
[no name] 9-Apr-12 8:26am    
It is YOUR project you should know what the primary keys are for YOUR tables.
Bishnu Tewary 9-Apr-12 8:54am    
Sorry you are not getting my question,
My question is I am passing the Tablename to that function

and i am not like to use switch statement or if-else

Entity obj = context.entity.FirstOrDefault(e => e.key == key);

Where here entity is the table but how could we use , because here entity depends on the tablename parameter passed from the calling method.
[no name] 9-Apr-12 9:11am    
Sorry you are not getting my answer. Go learn more about EF and maybe you will. You are making thing more difficult than they need to be by trying to make a one size fits all solution to a problem that doesn't exist.
Bishnu Tewary 9-Apr-12 9:19am    
Thank you mark for your time . :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900