FetchEntity on non primary field

Posts   
 
    
Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 14-Nov-2007 15:45:53   

I need help on this. Maybe I did not understand the documentation correctly but how to I do this:

CarrierEntity CarrierID (Pk) CarrierName (indexed)

How do I fetch one entity via the CarrierName not the CarrierID? I would like it to return null or the correct value.

What am I missing?

Thanks, Wade

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-Nov-2007 16:14:15   

In the database, a Unique key should be used for CarrierName. Then you may referesh the catalog and re-generate the code.

Then you may use the generated UniqueConstraint fetch method, as shown in the manual's section Using a unique constraint's value, under "Using the generated code -> SelfServicing / Adapter -> Using the entity classes".

Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 14-Nov-2007 16:25:53   

The data is not always unique. I would like to basically do the same as this:

Select * from Carrier where CarrierName = "UPS";

How does that work in just an entity?

This might be a bad example but there are other fields I would like to do the same on that will only have an Index on it not the unique constraint.

Wade

What about this one? confused

Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 14-Nov-2007 16:29:12   

I did another test where the field was a unique constraint and I am getting an error message that says: ORMOutOfSync Exception. Everytime. I can filter via a collection so I know it is returning data but the FetchUsingUC is not working.

Wade

Also: We do have the latest build installed of LLBLGenPro 2.5 (10.25.2007)

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-Nov-2007 16:59:25   

Select * from Carrier where CarrierName = "UPS"; How does that work in just an entity?

You may use the adapter's FetchNewEntity method. Please look it up in the Reference manual.

I did another test where the field was a unique constraint and I am getting an error message that says: ORMOutOfSync Exception. Everytime. I can filter via a collection so I know it is returning data but the FetchUsingUC is not working.

Please elaborate more on this, with code snippet and stack trace.

Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 14-Nov-2007 17:14:26   

I don't think FetchNewEntity is correct for the case I mention. In the documentation it is used with either a Related Entity or a Inherited Entity (PolyMorphic).

Again I would like to be able to return an entity based on a non-unique indexed column for one table without having to do it via a collection. I realize I could do it via a stored proc but then I am bound to changing that proc if I move to another database provider. Shouldn't there be a FetchEntityUsingIndex method??

confused

Wade

Wade
User
Posts: 76
Joined: 15-Jun-2004
# Posted on: 14-Nov-2007 19:40:08   

Otis, Help???

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Nov-2007 04:14:48   

Hi Wade,

Wade wrote:

I don't think FetchNewEntity is correct for the case I mention. In the documentation it is used with either a Related Entity or a Inherited Entity (PolyMorphic).

As a matter of fact, you also can use **FetchNewEntity **to perform the query you are looking for.

See, here is an example (I want to fetch the first customer that fit my criteria):

IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(CustomerFields.Country == "USA");

CustomerEntity theCustomer = new CustomerEntity();
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    theCustomer = (CustomerEntity) adapter.FetchNewEntity(new CustomerEntityFactory(), filter);             
}

Here is the generated code for that fetch:

SELECT TOP 1 [Northwind].[dbo].[Customers].[CustomerID] AS [CustomerId], [Northwind].[dbo].[Customers].[CompanyName], [Northwind].[dbo].[Customers].[ContactName], [Northwind].[dbo].[Customers].[ContactTitle], [Northwind].[dbo].[Customers].[Address], [Northwind].[dbo].[Customers].[City], [Northwind].[dbo].[Customers].[Region], [Northwind].[dbo].[Customers].[PostalCode], [Northwind].[dbo].[Customers].[Country], [Northwind].[dbo].[Customers].[Phone], [Northwind].[dbo].[Customers].[Fax] FROM [Northwind].[dbo].[Customers]  WHERE ( ( [Northwind].[dbo].[Customers].[Country] = @Country1))
    Parameter: @Country1 : String. Length: 15. Precision: 0. Scale: 0. Direction: Input. Value: "USA".

Hope helpful wink

David Elizondo | LLBLGen Support Team