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
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
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.