Lookup Question

Posts   
 
    
Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 02-Aug-2004 20:18:44   

What is the best method with the generated code to do the following:

Say I have a table called ROLES

What I want to do is to do a quick check to see if a rolename = "Admin" exists in the table?

What I want is for it to just return true or false.

Thanks, Wade

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39826
Joined: 17-Aug-2003
# Posted on: 02-Aug-2004 20:33:30   

create a filter and pull the object with that filter from the db. If the object could be loaded, it exists.

Frans Bouma | Lead developer LLBLGen Pro
Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 02-Aug-2004 21:03:25   

So is there an exception thrown if it does not exist? Or a boolean returned?

Also on the filter do I have to create a Index or Constraint on the Rolename column to do the ConstructFilter..... ?

Wade

bertcord avatar
bertcord
User
Posts: 206
Joined: 01-Dec-2003
# Posted on: 02-Aug-2004 21:17:14   

Wade wrote:

So is there an exception thrown if it does not exist? Or a boolean returned?

Also on the filter do I have to create a Index or Constraint on the Rolename column to do the ConstructFilter..... ?

Wade

I would suggest taking a look at the documents. From the LLBLGenProRefrenceManual

DataAccessAdapterBase.FetchEntity Method Fetches an entity from the persistent storage into the passed in Entity2 object using a primary key filter. The primary key fields of the entity passed in have to have the primary key values. (Example: CustomerID has to have a value, when you want to fetch a CustomerEntity from the persistent storage into the passed in object)

[Visual Basic] Overridable Public Function FetchEntity( _ ByVal entityToFetch As IEntity2 _ ) As Boolean Implements _ IDataAccessAdapter.FetchEntity [C#] public virtual bool FetchEntity( IEntity2 entityToFetch ); Parameters entityToFetch The entity object in which the fetched entity data will be stored. The primary key fields have to have a value. Return Value true if the Fetch was succesful, false otherwise

Implements IDataAccessAdapter.FetchEntity

Remarks Will use a current transaction if a transaction is in progress, so MVCC or other concurrency scheme used by the database can be utilized

See Also DataAccessAdapterBase Class | SD.LLBLGen.Pro.ORMSupportClasses Namespace

Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 02-Aug-2004 22:28:31   

Here is a copy of the code I am using to try to validate whether a role exists or not:

        private bool ValidWebRole( string role )
        {
            // Check for a zero value.
            if ( role.Length == 0 )
            {
                return false;
            }


            // Declare a DataAccessAdapter.
            DataAccessAdapter da = new DataAccessAdapter();

            RelationPredicateBucket filterBucket = new RelationPredicateBucket();
            IPredicate filterExpression = PredicateFactory.CompareValue( WebRoleFieldIndex.Role, ComparisonOperator.Equal, role );
            filterBucket.PredicateExpression.Add( filterExpression );
            WebRoleEntity webRoleEntity = new WebRoleEntity();


            // Check the database to see if the webRoleID is valid i.e. if it exists.
            if ( ! da.FetchEntity(webRoleEntity() ) ) 
            {
                return false;
            }
    
            da.CloseConnection();

            return true;
        }

How should this be done?

Thanks, Wade

Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 02-Aug-2004 22:48:36   

Never Mind I figured it out.

Thanks, Wade