Adapter - FK not saving

Posts   
 
    
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 27-Jul-2007 22:16:55   

For some reason I am having trouble with the fairly straight forward task of creating a new entity with a relationship. I have a Product Entity and a Variant Entity with a 1:m relationship. I create a new variant in the following way:

public static void AddNewVariant(string descriptor, ProductEntity product)
{
      VariantEntity variant = new VariantEntity();
      variant.Descriptor = descriptor;
      variant.Product = product;

      DataAccessAdapter adapter = new DataAccessAdapter();
      adapter.SaveEntity(product);
}

In debug mode I can see that the fk_id is set correctly in the entity, but for some reason when the adapter saves the entity, the setting of the foreign key value has an output direction and not an input direction, thus a SQL Null error is thrown. Here is the generated SQL:

Query: INSERT INTO [dev_QubicECommerce].[dbo].[ProductVariant] ([Descriptor])  VALUES (@Descriptor);SELECT @Pk_ID=SCOPE_IDENTITY();SELECT @Fk_ProductID=SCOPE_IDENTITY()
    Parameter: @Pk_ID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: <undefined value>.
    Parameter: @Fk_ProductID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: 1.
    Parameter: @Descriptor : AnsiString. Length: 50. Precision: 0. Scale: 0. Direction: Input. Value: "Test Descriptor".

I am using Adapter templates with version 2.5 Beta.

Apologies if I am just missing something obvious. Thanks for any help

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-Jul-2007 20:24:43   

Hi, I'm little lost here. What is supposed your code do?

  1. Save a new VariantEntity related to a product?
  2. Save a ProductEntity and its new VariantEntity (recursive save) ?
David Elizondo | LLBLGen Support Team
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 29-Jul-2007 01:28:44   

Hi David

Thanks for your response

The product entity already exists (and is already persisted in the database). The method (in the above example) is intended to take an existing product entity, and do the following:

  • Create a new variant
  • Create relationship between Variant Entity and Product Entity (a Product can have many Variants)
  • Save the variant

However, this never seems to save the foreign key to the database. The SQL output has been listed above - which i put in to show that for some reason the foreign key is an output parameter, rather than an input parameter as i would have expected. This may help with diagnosis!

I am basically attempting to follow the LLBLGenPro documentation (in the "Using the Entity Classes" section), which states:

Instantiate a Customer entity, add a new Order object to its Orders collection. Now add OrderDetails objects to the new Order object's OrderDetails collection,. You can simply save the Customer entity and all included new/'dirty' entities will be saved and any PK-FK relations will be updated/synchronized automatically.

Thanks for any help.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Jul-2007 21:22:46   

jshallard wrote:

  • Create a new variant
  • Create relationship between Variant Entity and Product Entity (a Product can have many Variants)
  • Save the variant

If you need to save VariantEntity you have two options. In both the PK-FK works:

A. Save VariantEntity

public static void AddNewVariant(string descriptor, ProductEntity product)
{
     VariantEntity variant = new VariantEntity();
     variant.Descriptor = descriptor;
     variant.Product = product;

     DataAccessAdapter adapter = new DataAccessAdapter();
     adapter.SaveEntity(variant);
}

B. Save ProductEntity recursively so new associated VariantEntity will be saved as well.

public static void AddNewVariant(string descriptor, ProductEntity product)
{
     VariantEntity variant = new VariantEntity();
     variant.Descriptor = descriptor;
     variant.Product = product;

     DataAccessAdapter adapter = new DataAccessAdapter();
     adapter.SaveEntity(product, true);
}

Hope helpful simple_smile

David Elizondo | LLBLGen Support Team
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 30-Jul-2007 19:40:11   

This thread was marked as done a little prematurely - it was marked as done, though my problem is still not solved!

David, thanks for your response. I have tried all your suggestions, but have the same problem. The issue seems to be that the FK is marked as an output parameter in SQL:

    Parameter: @Pk_ID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: <undefined value>.
    Parameter: @Fk_ProductID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: 1.

If there is any other debugging information i can provide to help diagnose the problem, please let me know.

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 31-Jul-2007 09:02:26   

SELECT @Fk_ProductID=SCOPE_IDENTITY()

Please make sure the FK field is not set to be an Identity column. - Check the database schema - And check that field properties in the LLBLGenPro Designer, make sure the "Is Identity" chekbox is unchecked.

jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 31-Jul-2007 21:08:22   

Bingo!

Thanks Walla - for some reason the "Is Identity" checkbox was set in the LLBLGenPro Designer. I removed this and it works now.

I am sure I did not set this manually, and am fairly certain that this was never set in the database. I remember this happening once before. I am wondering in perhaps in some circumstances when changes are made to the database, and the catalogue is refreshed - this can flip on unintentionally?

No matter to me now as my problem is fixed. I just though I would mention it in case anyone else experienced the same.

Thanks again for your help