I have a simple datamodel - two tables
Product table
ID - string
DESC - string
CATEGORY_ID - string
Category table
ID - string
DESC - string
I have a foeign key constraint defined in the database for the product table Category_ID must equal Category.ID
I create my entity model, service, service reference, run datasvcutil etc. all is working for the most part
When I try to create a new Product I use
dim aNewProduct as new PRODUCT
aNewProduct.ID = "TEST"
aNewProduct.DESC = "TEST"
dim aCategory as new CATEGORY
aCatogory.ID = 'EXSTING CATEGORY ID'
aNewProduct.Category = aCategory
myService.AddToPRODUCT(aNewProduct)
My new product row shows up in the database but the CATEGORY_ID is null
I've also tried running a linq query and getting a CATEGORY entity and setting the aNewProduct.Category to the existing CATEGORY entity. I have also tried the simple aNewProduct.Category_ID = 'EXSTING CATEGORY ID'
Does anyone have a way to do this?
Thanks
addTo where the inserted table has a foreign key
Re: addTo where the inserted table has a foreign key
What relations between tables ?
May be:
dim aCategory as new CATEGORY
aCatogory.ID = 'EXSTING CATEGORY ID'
aCatogory.DESC = 'CVGFJG'
dim aNewProduct as new PRODUCT
aNewProduct.ID = "TEST"
aNewProduct.DESC = "TEST"
aNewProduct.Category = aCategory
myService.AddToCategory(aCatogory)
myService.AddToPRODUCT(aNewProduct)
May be:
dim aCategory as new CATEGORY
aCatogory.ID = 'EXSTING CATEGORY ID'
aCatogory.DESC = 'CVGFJG'
dim aNewProduct as new PRODUCT
aNewProduct.ID = "TEST"
aNewProduct.DESC = "TEST"
aNewProduct.Category = aCategory
myService.AddToCategory(aCatogory)
myService.AddToPRODUCT(aNewProduct)
If you call SaveChanges() method without adding to the context the category you have created in the code
, the context treats this situation as if you are trying to add an entity whose parent code is not in the context, and this parent code is set to null.
If you want to assign to the new product a parent code that corresponds to an already existing category, you should attach it to the context
beforehand, by selecting it, like in the following code:
Code: Select all
dim aCategory as new CATEGORY
aCatogory.ID = 'EXSTING CATEGORY ID'
If you want to assign to the new product a parent code that corresponds to an already existing category, you should attach it to the context
beforehand, by selecting it, like in the following code:
Code: Select all
aNewProduct.Category = (From c in Context.Categories Where c.ID = "EXISTING CATEGORY ID"
Select c).First(Of Category)()