Developer forum

Forum » Development » Codefirst item problems

Codefirst item problems

Casper Andersen
Reply

Hello DynamicWeb

I am having some problems with my codefirst items that i'm hoping you can help me with.

I had an item type that i recreated with codefirst and everything worked fine, i can view it and so on, but when i try to create a new instance of it, it wont let me because of a foreign key to the itemtype Layout which Course inherited from.

So i also created the Layout with codefirst and that worked fine aswell, but when i inherit from Layout in my first itemtype and then Layout inherits from ItemEntry, i just can not get my first item to use the inherit from attribute which is needed.

Also stuff like pageDefaultView and useCache dont seem to be available in dynamicweb 8.9, i tried it in 9.1 and it let me use [ItemCache] but it still did not set useCache to true, is there something i'm doing wrong or is it a bug?


Replies

 
Nicolai Pedersen
Reply

Hi Casper

Please check out the attached example of an item that uses inheritance.

BR Nicolai

 
Casper Andersen
Reply

You gave me these items not too long ago and i looked at them and still i cant get this to work, also when i use the items you linked as are, i still dont see that they are inherited from eachother at least not where one item goes under another item in the management center.

Some other things i noticed:

1. [ItemCache] is not available in dynamicweb 8, only in version 9 but still when applying [ItemCache] as an attribute to the item, the useCache attribute in the xml is still set to false and this is reflect in the management center.

2. ProductAttribute is not available in DynamicWeb 9 or maybe it is inside a package which i dont know, although i checked the class library for version 9 and could not find it there

Also as a side note i tried both your item types and my own in version 8.9 and 9.1 and there was no difference.

 
Casper Andersen
Reply

Also i can see in the example items, that the save method is overriden and ItemContext is passed in, how do get the item context when i'm creating a new item?

 
Nicolai Pedersen
Reply

Hi Casper

You need to run a DW8.9.* or 8.8.1.27 to get full support for all of the things in the examples, including the ItemCache attribute.

You do not need to have an item context when creating new items - or you can create one with New ItemContext(DBobject) - you need to pass in a page or paragraph instance that the item resides on (if it is a area, page or paragraph item) in the contructor.

The product attribute is in Dynamicweb.Ecommerce.dll in DW9 since it is Ecommerce related (Dynamicweb.Ecommerce.Content.Items.Annotations)

BR Nicolai

 
Casper Andersen
Reply

Ok that gives me an idea of what is wrong. but in order to fix that problem i need to fix another problem, which is that when i am trying to create a new instance and save my codefirst item.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK__ItemType_Cou__Id__625078B6". The conflict occurred in database "xxxx", table "dbo.ItemType_Layout", column 'Id'.

Because my item inherits from Layout, so i try to create a new instance of my Layout item first which i have converted to a codefirst item so i could inherit from it.

Also i have checked all over the place, i do not see ItemCache available in 8.9 as an attribute, when inheriting from ItemEntry

 
Casper Andersen
Reply

Ok so i get everything to work.

Now i'm not sure if what i did is the correct way of doing things but it has done the job for me right now so thats at least something.

My problem was that the 2 items i have as codefirst items where created to start with in the dynamicweb admin panel, so i assumed that i could just create them in codefirst and take over, but the problem is dynamicweb creates a foreign key to keep track of the relations between the 2 item types, but when i created them with codefirst and inherited from one another then that automatically chained them together, so i get the properties of the inherited item and can use them and set them.

So far everything is working like this, if it should happen to fail ill write in here again and advice against this.

 
Nicolai Pedersen
Reply

Hi Casper

Thanks for explaining. Glad you got it to work!

BR Nicolai

 

You must be logged in to post in the forum