This is a migrated thread and some comments may be shown as answers.

Linked objects inserting a new row in the table for every link

1 Answer 56 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nigel
Top achievements
Rank 1
Nigel asked on 27 Feb 2009, 05:35 AM
Hi, I have a persistent class products which has a link to a persitent class TaxCode, I am linking all my products to one instance of the an object instance of TaxCode however when I add the product to the collection it is creating a new row in the TaxCode table for each Product->TaxCode link where I would have thought it would simple reference the existing TaxCode row (this is also occuring for all the other object types such a suppliers, but I think if I can fix it for TaxCodes I should be able to fix it for all of them). The class definition is below.

using

 

System;

 

using

 

System.Collections.Generic;

 

using

 

System.Linq;

 

using

 

System.Text;

 

using

 

Telerik.OpenAccess;

 

namespace

 

SiteDataObjects

 

{

[

Persistent(IdentityField="productID")]

 

 

public class Product

 

{

 

protected long productID;

 

 

protected string productTitle;

 

 

protected string productShortDescription;

 

 

protected string productDetails;

 

 

protected Manufacturer manufacturer;

 

 

protected Category category;

 

 

protected IList<SupplierProduct> supplierItems = new List<SupplierProduct>();

 

 

protected Supplier preferredSupplier;

 

 

protected bool itemIsSold;

 

 

protected int stockLevel;

 

 

protected TaxCode saleTax;

 

 

protected double margin;

 

 

protected bool exceedRRP = false;

 

 

protected ShippingValues shippingValues;

 

 

protected IList<Product> relatedProducts = new List<Product>();

 

 

protected IList<RelatedGroup> relatedGroups = new List<RelatedGroup>();

 

 

protected IDictionary<string, ProductCodes> productCodes = new Dictionary<string, ProductCodes>();

 

 

protected IList<Image> images = new List<Image>();

 

 

protected IDictionary<string, string> otherInfo = new Dictionary<string, string>();

 

 

protected Product supersedingProduct;

 

 

protected Product supersededProduct;

 

 

protected bool newProduct = false;

 

 

protected bool discontinued = false;

 

[

FieldAlias("productID")]

 

 

public long ProductID

 

{

 

get { return productID; }

 

 

set { productID = value; }

 

}

[

FieldAlias("productTitle")]

 

 

public string ProductTitle

 

{

 

get { return productTitle; }

 

 

set { productTitle = value; }

 

}

[

FieldAlias("productShortDescription")]

 

 

public string ProductShortDescription

 

{

 

get { return productShortDescription; }

 

 

set { productShortDescription = value; }

 

}

[

FieldAlias("productDetails")]

 

 

public string ProductDetails

 

{

 

get { return productDetails; }

 

 

set { productDetails = value; }

 

}

[

FieldAlias("manufacturer")]

 

 

public Manufacturer Manufacturer

 

{

 

get { return manufacturer; }

 

 

set { manufacturer = value; }

 

}

[

FieldAlias("category")]

 

 

public Category Category

 

{

 

get { return category; }

 

 

set { category = value; }

 

}

[

FieldAlias("supplierItems")]

 

 

public IList<SupplierProduct> SupplierItems

 

{

 

get { return supplierItems; }

 

}

[

FieldAlias("saleTax")]

 

 

public TaxCode SaleTax

 

{

 

get { return saleTax; }

 

 

set { saleTax = value; }

 

}

[

FieldAlias("margin")]

 

 

public double Margin

 

{

 

get { return margin; }

 

 

set { margin = value; }

 

}

[

FieldAlias("exceedRRP")]

 

 

public bool ExceedRRP

 

{

 

get { return exceedRRP; }

 

 

set { exceedRRP = value; }

 

}

[

FieldAlias("shippingValues")]

 

 

public ShippingValues ShippingValues

 

{

 

get { return shippingValues; }

 

 

set { shippingValues = value; }

 

}

[

FieldAlias("relatedProducts")]

 

 

public IList<Product> RelatedProducts

 

{

 

get { return relatedProducts; }

 

}

[

FieldAlias("relatedGroups")]

 

 

public IList<RelatedGroup> RelatedGroups

 

{

 

get { return relatedGroups; }

 

}

[

FieldAlias("productCodes")]

 

 

public IDictionary<string, ProductCodes> ProductCodes

 

{

 

get{return productCodes;}

 

}

[

FieldAlias("images")]

 

 

public IList<Image> Images

 

{

 

get { return images; }

 

}

[

FieldAlias("preferredSupplier")]

 

 

public Supplier PreferredSupplier

 

{

 

get { return preferredSupplier; }

 

 

set { preferredSupplier = value; }

 

}

[

FieldAlias("itemIsSold")]

 

 

public bool ItemIsSold

 

{

 

get { return itemIsSold; }

 

 

set { itemIsSold = value; }

 

}

[

FieldAlias("stockLevel")]

 

 

public int StockLevel

 

{

 

get { return stockLevel; }

 

 

set { stockLevel = value; }

 

}

[

FieldAlias("otherInfo")]

 

 

public IDictionary<string, string> OtherDetails

 

{

 

get { return otherInfo; }

 

}

[

FieldAlias("supersedingProduct")]

 

 

public Product SupersedingProduct

 

{

 

get { return supersedingProduct; }

 

 

set { supersedingProduct = value; }

 

}

[

FieldAlias("supersededProduct")]

 

 

public Product SupersededProduct

 

{

 

get { return supersededProduct; }

 

 

set { supersededProduct = value; }

 

}

[

FieldAlias("newProduct")]

 

 

public bool New

 

{

 

get { return newProduct; }

 

 

set { newProduct = value; }

 

}

[

FieldAlias("discontinued")]

 

 

public bool Discontinued

 

{

 

get { return discontinued; }

 

 

set { discontinued = value; }

 

}

 

 

public Product() { }

 

 

 

 

public double SalePrice

 

{

 

get

 

{

 

// Will calculate from suppliers

 

 

return 0.0 + DateTime.Now.Second;

 

}

}

 

}

}

 

using

 

System;

 

 

 

 

using

 

System.Collections.Generic;

 

 

 

 

using

 

System.Linq;

 

 

 

 

using

 

System.Text;

 

 

 

 

using

 

Telerik.OpenAccess;

 

 

 

 

namespace

 

SiteDataObjects

 

{

[

Persistent(IdentityField="taxCodeID")]

 

 

public class TaxCode

 

 

 

 

 

{

 

protected long taxCodeID;

 

 

protected string code;

 

 

protected double amount;

 

[

FieldAlias("taxCodeID")]

 

 

public long TaxCodeID

 

{

 

get { return taxCodeID; }

 

 

set { taxCodeID = value; }

 

}

[

FieldAlias("code")]

 

 

public string Code

 

{

 

get { return code; }

 

 

set { code = value; }

 

}

[

FieldAlias("amount")]

 

 

public double Amount

 

{

 

get { return amount; }

 

 

set { amount = value; }

 

}

 

public TaxCode()

 

{

}

 

public TaxCode(string Code, double Amount)

 

{

 

this.Code = Code;

 

 

this.Amount = Amount;

 

}

 

}

 

}

all IDs are configured to use Autogenerated HIGHLOW type.

Thanks
Nigel

1 Answer, 1 is accepted

Sort by
0
Jan Blessenohl
Telerik team
answered on 02 Mar 2009, 08:56 AM
Hi Nigel,
Your class definition looks good.

The only idea I have about your error is that you are generating new TaxCode objects that will get individual ids because of the high-low key generator.

To generate a new Product you have to get the existing TaxCode out of the database using a query. This is the only object that you should assign to the product TacCode property. If you generate new objects instead, even if you set the right id, the objectscope.Add() call will overwrite that with a new generated id.

All the best,
Jan Blessenohl
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
General Discussions
Asked by
Nigel
Top achievements
Rank 1
Answers by
Jan Blessenohl
Telerik team
Share this question
or