FetchEntityUsingUniqueConstraint (Null)

Posts   
 
    
craigmain
User
Posts: 22
Joined: 17-Apr-2005
# Posted on: 02-May-2005 11:04:27   

Hi,

I have a unique constraint on and integer field (Reference) and a datetime field (End_Date).

I need to find the entity using FetchEntityUsingUniqueConstraint, with a supplied reference and an end_date of NULL. How do I use a null value here?

Regards Craig

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39932
Joined: 17-Aug-2003
# Posted on: 03-May-2005 11:24:56   

Formulate your own predicate expression and pass that to the method, which should be fairly easy: 1 comparevalue and one CompareNull predicate.

Be sure the fetch results in 1 row.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 4
Joined: 08-Jan-2006
# Posted on: 11-Jan-2006 10:15:02   

I have the same scenario as in the original post but I'm using the selfservicing template set and would like to have a constructor that returns the latest version (EndDate == null).

The FetchUsingUCXXX Method on the entity itself doesn't allow a predicate expression so how could I achieve this?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39932
Joined: 17-Aug-2003
# Posted on: 11-Jan-2006 12:10:26   

As teh field can potentially result in multiple instances (it's not a pk nor a unique constraint) you have to fetch an entitycollection and pick the first item.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 4
Joined: 08-Jan-2006
# Posted on: 11-Jan-2006 13:03:00   

As teh field can potentially result in multiple instances (it's not a pk nor a unique constraint)

I've added the fields as a unique contraint, the related JobEntity also has the corresponding method FetchUsingUCIdValidTo (IdValidTo being the name of my unique constraint, which is on the ID and ValidTo fields).

The problem is that I can't add a null value for one of the columns in the unique constraint through the generated method overloads since the method overloads only accept Int32 and DateTime params.

From SQL 2005 Books online ("about UNIQUE contraints"):

Also, unlike PRIMARY KEY constraints, UNIQUE constraints allow for the value NULL. However, as with any value participating in a UNIQUE constraint, only one null value is allowed per column.

I think this is the same problem craigmain ran into, my question is if there is an alternative for your suggestion above (Formulate your own predicate expression in FetchEntityUsingUniqueConstraint) for the SelfServicing template. The 'workaround' you suggest was what I had initially but it really felt like a workaround so I was looking for a cleaner solution. It wouldn't work in the constructor of an entity since I can't assign to "this", i.e.: this = new JobEntity().

Could you tell me if this problem is solved in the template set you're building specifically for 2.0 using nullable types, because basically my problem would be solved if I could somehow pass NULL to one of the parameters in the unique contraint...

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Jan-2006 14:25:52   

Hi Paul,

I can't determine whether you are trying to Fetch an Entity using a unique constraint or trying to Insert An Entity with Unique field of Null value.

If the earlier is the case, then please use Frans suggestion to formulate your own predicate expression and pass it to the Fetch method.

If the later is the case, then please use SetNewFieldValue().

Please refer to the paragraph titled "Entities, NULL values and defaults" in the LLBLGen Pro documentation "Using the generated code - Adapter/SelfServicing - Using the entity classes"