my first DB module

Topics: Writing modules
Jan 26, 2012 at 8:16 AM

my first Module requires a 'date'. When doing the BUILD firstly I got an error saying DataTime is not valid. I managed to change it to 'system.datatime'.
But then on  'get { return Record.Date; }' I get a conversion error 'cannot convert from date to string'.

? how do I define correctly a date ?
? can anybody refer me to a sample using dates ?

PS: I probably could solve this task using the LIST business in Orchard, however, I could not find a 'date' field in the LIST which I could use
to do e.g. a sort on it!

? any other hints (and your time) is appreciated,

 ed

using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using System;

namespace events.Models {
    public class eventsRecord : ContentPartRecord {
        public virtual string events { get; set; }
        public virtual double category { get; set; }
        public virtual string HomeTeam { get; set; }
        public virtual string GuestTeam { get; set; }
        public virtual DateTime Date { get; set; }
        public virtual string Captain { get; set; }
        public virtual string change { get; set; }
        public virtual string comment { get; set; }

      
    }

    public class eventsPart : ContentPart<eventsRecord> {
        [Required]
        public string events
        {
            get { return Record.events; }
            set { Record.events = value; }
        }

 .
 .
 .
      
        [Required]
        public string Date
        {
            get { return Record.Date; }
            set { Record.Date = value; }
        }
       
 .
 .
 .

Jan 26, 2012 at 12:45 PM

- What does your Migration look like?

- Your record should be named "EventsPartRecord". It's just the convention.

Jan 26, 2012 at 4:26 PM

Well, the 'migration.cs' file looks like below i.e. for me it looks empty!

what did I do:
1) I changed as you suggested 'ContentPartRecoed to eventsPartRecord'.
2) I ran 'codegen datamigration events' with below result (the Model 'events.cs' was like seen in first msg [except change]).
   question: do I need to do a BUILD before I run the 'datamigration'? where would I do that?
   PS: when I call vs2010 out of WebMatrix I get an error on 'about.csproj' first line (Object reference not set to an instance of an object.)
       However, the 'about' module runs fime when running the site!? weird!?

thanks for any hint directing me ...
ed 

using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;

namespace events {
    public class Migrations : DataMigrationImpl {

        public int Create() {


            return 1;
        }
    }
}

Change in Model 'events.cs':
public class eventsRecord : eventsPartRecord {
.
.
.

Jan 26, 2012 at 4:30 PM

The problem is that you have one defined as DateTime and the other defined as string.

It shouldn't build.

 

public virtual DateTime Date { get; set; }

[Required]
public string Date
{
    get { return Record.Date; }
    set { Record.Date = value; }
}

 

Regards,

Dave

Jan 26, 2012 at 6:43 PM

ooops ... my apology for the string/date thing but  thanks!

now, when doing 'codegen datamigration events' I have two scenarios:

1) when running with old 'migrations.cs' in events root folder (with the old empty file): codegen says - 'migrations.cs already exists in module events'
2)after deleting 'migrations.cs': codegen comes back with 'migratios.cs ' not found

when comparing with Bing.Maps I have: ' Bing.MapsDataMigration.cs' in folder 'DataMigrations'. In my case codegen says could not find ...\Modules\events\Migrations.cs ... and I do NOT have a DataMigration folder generated .. should it get generated when doing the 'codegen Module events?

what am I doing wrong here ?!

thanks for your patience ed

Jan 26, 2012 at 6:45 PM

Normally it's a Migrations.cs in the root of your module, but it doesn't get created by codegen module, only by codegen datamigration.

Perhaps after deleting migrations.cs you need to "Save All" which will update the .csproj to show that Migrations.cs isn't there anymore.

Jan 26, 2012 at 8:36 PM

well thanks I'm now able to do the 'datamigration'; but, 'save all' didn't do it ... I had to delete the' Migration.cs' file in events.csproj!

However the 'migration.cs' file still looks empty. and the Model in Model\events.cs looks like
see below code. I must be missing something else ...!

ed

root\Migrationon.cs:


using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;

namespace events {
    public class Migrations : DataMigrationImpl {

        public int Create() {


            return 1;
        }
    }
}

Model\events.cs:
using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using System;

namespace events.Models {
    public class eventsRecord : ContentPartRecord {
        public virtual string events { get; set; }
        public virtual double category { get; set; }
        public virtual string HomeTeam { get; set; }
        public virtual string GuestTeam { get; set; }
        public virtual DateTime Date { get; set; } 
        public virtual string Captain { get; set; }
        public virtual string change { get; set; }
        public virtual string comment { get; set; }
       }
    public class eventsPart : ContentPart<eventsRecord> {
        [Required]
        public string events
        {
            get { return Record.events; }
            set { Record.events = value; }
        }

        [Required]
        public double category
        {
            get { return Record.category; }
            set { Record.category = value; }
        }
        
       [Required]
        public string HomeTeam
        {
            get { return Record.HomeTeam; }
            set { Record.HomeTeam = value; }
        }
 
        [Required]
        public string GuestTeam
        {
            get { return Record.GuestTeam; }
            set { Record.GuestTeam = value; }
        }
       
        [Required]
        public DateTime Date
        {
            get { return Record.Date; }
            set { Record.Date = value; }
        }
       
        [Required]
        public string Captain
        {
            get { return Record.Captain; }
            set { Record.Captain = value; }
        }
        
        [Required]
        public string change
        {
            get { return Record.change; }
            set { Record.change = value; }
        }
        
         [Required]
        public string comment
        {
            get { return Record.comment; }
            set { Record.comment = value; }
        }
    }
}
Jan 26, 2012 at 9:22 PM

Well you haven't done the thing I said before which was to name your record class "EventsPartRecord". If you don't follow the conventions then automated tasks like code generation and db mapping can struggle. It might seem a bit strange but conventions are there for a reason!

Jan 27, 2012 at 8:53 AM

I fully understand your inpatience. I have done the code generation with below events.cs almost a week ago - and now again - and still get an empty 'Migrations.cs'.
In case below code is wrong please let me know where ... ?
.. appreciate your time, ed

using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using System;

namespace events.Models {
    public class eventsPartRecord : ContentPartRecord {
        public virtual string events { get; set; }
        public virtual double category { get; set; }
        public virtual string HomeTeam { get; set; }
        public virtual string GuestTeam { get; set; }
        public virtual DateTime Date { get; set; } 
        public virtual string Captain { get; set; }
        public virtual string change { get; set; }
        public virtual string comment { get; set; }
       }
    public class eventsPart : ContentPart<eventsPartRecord> {
        [Required]
        public string events
        {
            get { return Record.events; }
            set { Record.events = value; }
        }
.
.
.
Feb 2, 2012 at 3:50 PM

well, I'm still where I was before: an empty migration.cs. In the meantime I read again the relevant doc (writing content part) and started with my module from scratch!?

What I didn't explicitely do was the BUILD mentioned in the Doc! However, I started my App before I did the 'codegen datamigration evenements'.

below again part of my .cs file and the output!

thanks for guiding me here, ed

using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using System;

namespace evenements.Models {
    public class evenementsPartRecord : ContentPartRecord {
        public virtual string events { get; set; }
        public virtual double category { get; set; }
        public virtual string homeTeam { get; set; }
        public virtual string guestTeam { get; set; }
        public virtual DateTime date { get; set; } 
        public virtual DateTime dateToEnroll { get; set; } 
        public virtual string captain { get; set; }
        public virtual string change { get; set; }
        public virtual string comment { get; set; }
       }
    public class evenementsPart : ContentPart<evenementsPartRecord> {
       
        [Required]
        public double category
        {
            get { return Record.category; }
            set { Record.category = value; }
        }
using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;

namespace evenements {
    public class Migrations : DataMigrationImpl {

        public int Create() {


            return 1;
        }
    }
}
Feb 2, 2012 at 5:18 PM

Nothing looks wrong here, but without seeing the whole project it's hard to say.

The only thing obviously different to how I normally do things is capitalization - I'd recommend everything being PascalCase because it's just standard in C# and it all looks strange to me with lowercase ;) I have no idea if that will make a difference when generating migrations but since it's not something that will be tested for you never know.

Failing all that, you can always just write a migration by hand. See some existing examples in core modules for how it works. Personally I never bother with code generating migrations, they don't take long to write once you know what you're doing, and it only works the first time anyway - any schema changes or additions you'll have to handcode later on anyway.

Feb 4, 2012 at 4:27 PM

well, after a nightshift I found my problem of the empty 'migrations.cs'.

When adding drivers / handlers etc. to .csproj like 'Compile Include="Handlers\EveHandler.cs' I'm ok and the app compiles correctly.
I just wonder why e.g. 'migrations.cs' is added automatically and e.g. handlers / drivers are not!

? Is this a parameter somewhere ? is there a reason for this ?

I consider this closed
thanks again for your guys patience and help
ed

Feb 4, 2012 at 5:16 PM

Are you generating the handlers and drivers e.g. with Advanced Code Generation? I don't know if that adds to the .csproj automatically. Otherwise if you added them by hand then maybe you just closed VS without saving the project?