Developer forum

Forum » Development » Code First Items not generated

Code First Items not generated

Tom Kamphuis
Reply

Hi guys,

I'm experimenting with the code first approach to Items but failing on the very first step of the process. I've created a class (from the blogpost example) with only one property called heading. My class inherits from ItemEntry as shown below. After a build I'm checking the management center (I've seen the debugger hit the Application_Start method) for a freshly added Item under Item types only to find none. There are no generated xml files in /System/Items but I see the generated table in the database (ItemType_BlogPost)... What am I doing wrong?

Code:

public class BlogPost : ItemEntry
{
    public string Heading { getset; }
}

Cheers,
Tom


Replies

 
Tom Kamphuis
Reply

Guys, anybody any idea?

 
Morten Bengtson
Reply

Have you tried to add either the Name or Item attribute?

[Item("Blog Post","Some description")]
public class BlogPost : ItemEntry
{
    public string Heading { get; set; }
}
 
Tom Kamphuis
Reply

I've tried them all, without any succes...

[Name("Blog entry")]
    [Item]
    public class BlogPost : ItemEntry
    {
        [Group("Main fields")]
        [Name("Heading text")]
        [DefaultValue("Yet another blog post")]
        [Required]
        public string Heading { getset; }
 
        [Group("Content")]
        [Name("HTML Body")]
        [LongText]
        [Required]
        public string Body { getset; }
    }
 
Morten Bengtson
Reply

OK, I don't know whats going on. Maybe you can force it to pick up your changes if you click the "Refresh" button above the list of item types in Management Center.

Otherwise, have a look in the /Files/System/Log folder and see if any errors have occurred.

 
Nicolai Høeg Pedersen
Reply

Did you compile it and make sure an assembly is in /Bin?

And also check the log files as Morten suggests.

 
Tom Kamphuis
Reply

I even decompiled my own assemblies to make sure the object was in there... There are also no logfiles that say anything about an error occured during item creation. Is there some setting that I should set? Something like "use code-first approach for Item generation"?

 
Morten Bengtson
Reply

Most of the code-first issues I have experienced have been caused by changes to existing item types (like changing the property types).

Missing xml files will happen if you have chosen "Database" synchonization for item types, but your item types should still show up in Management Center.

I assume that you are running your DW installation on IIS when debugging, right? Otherwise you'll run into all sorts of problems.

Also, make sure that your site (application pool identity) has read/write access to your "Files" folder (and all subfolders).

I have tested the code you provided on a clean DW 8.6 installation and everyting worked as expected. What version of DW are you using? Has it been upgraded recently?

 
Tom Kamphuis
Reply

Hi Morten,

We're running a DW 8.6.1.8 version and I have Database synchronization for item types turned on. I see my coded Item type created a database table in the database (ItemType_BlogPost) but I don't see it show up in the management studio. I am debugging using IIS and the app pool has read/write access.

What could interfer with showing item types in the management section of DW, anybody?

Cheers,
Tom

 
Tom Kamphuis
Reply

When switching from "Database synchronization" to "Synchronize all" my item type suddenly showed up! I guess this isn't expected behaviour?

EDIT: My item type only shows up after I've changed the synchronization style as explained by Morten? But after recycling my app pool and logging into the admin again the item type is gone... until I change the synchronization style. Is there also a less buggy way of creating item types from code? 

 
Tom Kamphuis
Reply

Allright, now I'm totally confused... here's what I have done:

  1. After setting the synchronization settings to all I created two more fields in code (a DateTime field and another String field). I deleted a ItemType xml which was already generated (with the old BlogPost item type containing only two fields). I rebuilt my application and recycled the App pool.
  2. After that I went into the management section and checked the Item types. Nothing there as I expected so I went to the synchronization settings and changed them from "all" to "database only". When I returned to my item types I saw my generated item type BlogPost.
  3. I clicked the item type only to find the two former fields (heading and body) but not the new fields (publishdatetime and summary).
  4. I checked the item type in the database to see that the table contains fields for all BlogPost fields ([Id],[ItemInstanceType],[Heading],[Body],[Sort],[PublishDateTime],[Summary]).
  5. No xml file was created (as expected).

Is there some kind of caching going on? (Even after apppool recycle?) I'm really unsure to use this code-first approach but I kind of need it in this new project...

 
Nicolai Høeg Pedersen
Reply

Hi Tom

Set the sync to files only - database is when 2 developers work on the same item type using the UI. There is no caching after recycle. Maybe the code in your bin twice? Did you change assembly name or anything?

BR Nicolai

 
Tom Kamphuis
Reply

I can do that (although I like the sound of syncing through a shared database) but that doens't help me get to the bottom of all problems described above:

  1. Item types don't show up in Admin (only after changing the sync strategy)
  2. Changing the code of an item type doesn't necessarily change it in the Admin (even after rebuild / apppool recycle)

And no, as stated earlier I've checked, double checked and triple checked my deployed binaries. And I didn't rename my assemblies. Should I say FizzBin? ;-)

Besides the ItemType_BlogPost table and the ItemType_BlogPost xml file there is another database table called ItemTypeDefinitions which looks like it contains the same XML as outputted on the file? Could it be that the Admin is looking at that table? And why is it so hard to get a simple example from the developer website to work? 

After making a code change, rebuilding the Application and recycling the Application pool my item type looks very excotic throughout the saved data:

The class:

using Dynamicweb.Content.Items;
using Dynamicweb.Content.Items.Annotations;
 
namespace Jaarbeurs.PoC.Web.DynamicwebItems.Items
{
    [Name("Blog entry")]
    [Item]
    public class BlogPost : ItemEntry
    {
        [Group("Main fields")]
        [Name("Heading text")]
        [DefaultValue("Yet another blog post")]
        [Required]
        public string Heading { getset; }
 
        [Group("Content")]
        [Name("HTML Summary")]
        [LongText]
        [Required]
        public string Summary { getset; }
 
        [Group("Content")]
        [Name("Plaatje")]
        public string ImageUrl { getset; }
 
        [Group("Content")]
        [Name("HTML Body")]
        [LongText]
        [Required]
        public string Body { getset; }
    }
}

The ItemType_BlogPost database table:
/****** Script for SelectTopNRows command from SSMS  ******/
SELECT TOP 1000 [Id]
      ,[ItemInstanceType]
      ,[Heading]
      ,[Body]
      ,[Sort]
      ,[Summary]
  FROM [JaarbeursPoC].[dbo].[ItemType_BlogPost]

The ItemType_BlogPost XML file:
Attached to this post. (Same as XML in database table ItemTypeDefinitions)

The item type shown in through the Admin interface is the same as reflected in the XML file.

 
Nicolai Høeg Pedersen
Reply

Hi Tom

I've asked a developer to look into the matter and see if something is wrong, which this indicates. We will get back to you.

BR Nicolai

 
Vladimir
Reply

Hi Tom, 

Sorry about weird example - it is definitelly a bug - we will fix it asap (so tags shouldn't be mandatory)

But with "name" tag items are created fine - I can't reproduce errors.

I prepared an example with item type - could you run it on you solution please?

It's just copy of Item from your code, but with name "Aaa blog post"

1. ItemExampleClassLibrary.dll - contains "Aaa blog post" with 2 fields

2.ItemExampleClassLibrary.dll - contains "Aaa blog post" with 4 fields

Try please to run them (with synchronization "database") - is the problem reproduced?

About synchronization:

When synchronization "database" - item type definitions are stored in db table "ItemTypeDefinitions" and not appeared in Files

Best regards,

Vladimir

 

 
Peter Leleulya
Reply

I just noticed that Items that I try to create via the CMS als vanish after pushing the save button.
Probably the same bug?
See video (swf).
 

 
Tom Kamphuis
Reply

Hi Vladimir,

I see no difference when using your classes instead of ours... I've attached a video to show you what the code, database, filesystem and admin looks like on my system.

Cheers,
​Tom

 
Vladimir
Reply

Hi Tom,

But, I don't notice what wrong on video?

Sync all - item types are shown in admin (their definition stored in DB  and Files) - perfect

Sync DB - item types are shown in admin (their definition stored only in DB) - perfect

Best regards,

Vladimir

 

 
Vladimir
Reply

Hi Peter, 

Is it the same solution?

Best regards,

Vladimir

 
Tom Kamphuis
Reply

Hi Vladimir,

When I first enter the website I don't see any Item Types... I have to change the sync settings to show my already created Item Types. Isn't that a bit strange? Also, when altering the Item Type objects I need to change the sync settings again and again... I'd say that those changes should be automatically visible in the backend...

Cheers,
Tom

 
Peter Leleulya
Reply

Same solution.

 
Nicolai Høeg Pedersen
Reply

Hi Peter and Tom

We cannot reproduce this issue. I think we need a full copy in order to investigate further.

Thanks, Nicolai

 
Peter Leleulya
Reply

I've updated the solution to the latest, 8.6.1.10, but the problem remains.
I can not create an Item at all.
I will create a case to provide you with a copy of the solution and database as requested.

 
Tom Kamphuis
Reply

Hi Guys,

We're encountering this issue on multiple solutions now. On a clean installation where we use the complete DW application there is no problem, we can create our item types without any issue. When our application is a custom solution things change. We can create an item type, which is created in the database but isn't shown in the admin. It seems to be a database read issue or maybe some locked file due to TFS on our custom application? We did some testing and could exclude the files directory, globalsettings and web.config but couldn't find the exact problem. We also added the IUSR and Network Service users to the folder security settings of the Files and Application folders but that didn't help us either. 

Anybody any idea?

Thanks!

 
Nicolai Høeg Pedersen
Reply

Hi Tom

TFS adds readonly to all folders and files. That is an issue if /Files/System/Items is readonly

 
Peter Leleulya
Reply

We have settings to databse only which, as we tested, doesn't touch the files directory.
We tried with an standard non custom dw setup and clean db with settings to database only.
It dreated the item which was created in the database, no system items folder appeared and the item was visible in the admin.

IN our custom application it creates the item in the database, but doesn't see it.
We also tried removing read only from files directory and that didnt't help :(

 
Nicolai Høeg Pedersen
Reply

In your team explorer, try to check out the folder - not the files, but the entire folder...

 
Tom Kamphuis
Reply

Ok, after some serios debugging and messing around with my solution I found the following: when you create a custom Global.Asax with GlobalAsaxHandler calls as shown below the application fails to show the ItemTypes in the Admin section and when you create a new ItemType the application fails to save the XML in the ItemTypeDefinition table.

public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            GlobalAsaxHandler.Application_Start(sender, e);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            GlobalAsaxHandler.Session_Start(sender, e);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            GlobalAsaxHandler.Application_BeginRequest(sender, e);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            GlobalAsaxHandler.Application_AuthenticateRequest(sender, e);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            GlobalAsaxHandler.Application_Error(sender, e);
        }

        protected void Session_End(object sender, EventArgs e)
        {
            GlobalAsaxHandler.Session_End(sender, e);
        }

        protected void Application_End(object sender, EventArgs e)
        {
            GlobalAsaxHandler.Application_End(sender, e);
        }
    }

We need to add extra functionality to the Session Start of the application and thus need a custom Global.asax. When I remove our custom global.asax and replace it with the normal one everything works, without changing Files folders or whatsoever.

My question is: is there any way to accomplish our desired behaviour (for ItemTypes and in the Global.asax)? And is the problem described above registered as a bug?

Thanks!
Tom

 
Morten Bengtson
Reply
This post has been marked as an answer

I think your Global.asax.cs is missing an event handler...

public void Application_OnPreRequestHandlerExecute(object sender, EventArgs e)
{
    GlobalAsaxHandler.Application_OnPreRequestHandlerExecute(sender, e);
}
Votes for this answer: 1
 
Tom Kamphuis
Reply

Facepalm... walks away, quietly...

Thanks Morten, that was keeping our ItemTypes from appearing in the Admin!

 
Nicolai Høeg Pedersen
Reply

Hi Tom & Morten

Oh, that was a long running thread. So glad you figured it out, thanks Morten for the fresh eyes on the problem...!

Have a nice weekend.

BR Nicolai

 

You must be logged in to post in the forum