Creating Custom Relation Across Tables

Posts   
 
    
greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 23-Aug-2012 20:22:27   

Hi,

Here's my scenario:

I'm using the LLlbgen Designer and Llblgen run-time engine version 3.

I have a "domain" table (Llblgen Entity) for my web-application. Each domain (a company account) has multiple (user) Logins.

Tables (Llblgen Entities): Domain - Columns: Id, UsesMyIndentityProvider (boolean) Login - Columns: Id, DomainId (m logins for 1 domain), LoginName

Each Login table has a LoginName field (the user's login name). In the Login table, the composite of LoginName + DomainId has a unique key defined.

All is good so far...

Here's what I'm trying to achieve: A Login.LongName needs to be unique across all Domains where Domain.UsesMyIdentityProvider = true, need to be unique.

I'm looking for a "constraint" like: Unique Key where Login.LoginName is unique where Domain.UsesMyIdentityProvider == true.

Can I set this up as a "Custom Relation" in Llblgen?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Aug-2012 07:04:09   

You can't create a "design" relation based on data on a related table, not in DB and not at LLBLGen. You can create a custom relation at runtime to get LoginName unique values between LoginName,Domain.Id. But that is just for fetch, you can do that using Custom Filters for Entity Relations.

However, you want to have unique values on that context in your DB. To do that you will have to perform a check before insert/update a Login, but I don't see how the custom relation will be useful here, since you can do it with some query filter:

shouldWeSaveLogin = IsLoginValidForSave(myLogin.LoginName, myLogin.DomainId);

if (shouldWeSaveLogin )
{
     adapter.SaveEntity(myLogin);
}
else
{
     // handle it
}
public bool IsLoginValidForSave(string loginName, int domainId)
{
     var filter = new RelationPredicateBucket(
          LoginFields.LoginName == loginName
          & LoinFields.DomainId == domainId
          & DomainFields.UsesMyIdentityProvider == true);

     var count = new DataAccessAdapter().GetDbCount(new EntityCollection<LoginEntity>(), filter);
     return count == 0;
}
David Elizondo | LLBLGen Support Team