This project is read-only.

Data Migration - Syntax - Id setting Identity, Increment and seed

Topics: Writing modules
Apr 6, 2011 at 1:46 PM
Edited Apr 6, 2011 at 1:48 PM


Can anyone point me in the direction of any documentation that lets me know the syntax for settining the identity, Increment and seed  from Migration.cs ?

I have seen 

        table => table
            .Column<int>("Id", column => column.PrimaryKey().Identity())
            .Column<string>("Code", column => column.WithLength(2))

I have no Id field in my Part Or Record prior to running codegen to create the migration but have played around with it enough this morning to see 
that Orchard automatically assigns an Id field in the generated tables. Is there some reference (or forum post) somewhere on the syntax to alter the
 generated tables in perhaps an updateFrom after the initial table creation?

Also after playing around, creating some tables through migration, manually deleting them and trying to recreate them again I'm under 
the impression that Orchard also stores some information on these tables somewhere else as it causes issues trying to recreate them 
again after deletion. Can anyone tell me in which tables this information could be stored so perhaps I could look into manually ripping the 
entries out (if its not going to cause problems elsewhere)?

Thanks in advance
Apr 6, 2011 at 3:41 PM
Edited Apr 6, 2011 at 3:42 PM

Are you trying to create a Content Part here?

Your classes should look like:

public class StatePartRecord : ContentPartRecord {



public class StatePart : ContentPart<StatePartRecord>
{ ... }

And finally assuming the above inheritances are correct, your codegen'd migration should look like:

        table => table
// Don't need Id column with .ContentPartRecord(), it's already declared!
// .Column<int>("Id", column => column.PrimaryKey().Identity()) .Column<string>("Code", column => column.WithLength(2)) .Column<string>("Name") );

I think the example you were looking at was for an ordinary table, not a content-joined one.

Apr 6, 2011 at 8:11 PM

Hi Pete.

Yea, I'm looking at eventually building a module that will act as a kind of internal Company Address book that will bolt on to registered users in certain roles. At the moment tho I'm just trying to go through the process of creating a basic part of that to gain a better understand how all of this works.

I currently have.... 


  public class PersonPartRecordContentPartRecord

        public virtual FormalTitleRecord FormalTitleRecord { getset; }
        public virtual string FirstName { getset; }
        public virtual string MiddleName { getset; }
        public virtual string LastName { getset; }
        public virtual string DateOfBirth { getset; }


 public class PersonPart :ContentPart<PersonPartRecord>

public class FormalTitleRecordContentPartRecord

        public virtual string FormalTitle { getset; }


After the data migration I get...

public int Create() {
			// Creating table FormalTitleRecord
SchemaBuilder.CreateTable("FormalTitleRecord", table => table

// Creating table PersonPartRecord
SchemaBuilder.CreateTable("PersonPartRecord", table => table


This creates the required tables and also ads an int as the PK but I just wanted to figure out how to modify the values of the PK either after or during creation.


best regards


Apr 6, 2011 at 8:12 PM


Apr 6, 2011 at 8:13 PM

Why what? :unsure: lol

Apr 6, 2011 at 8:14 PM

:) Why do want to modify the value of the PK?

Apr 6, 2011 at 8:24 PM
Edited Apr 6, 2011 at 8:25 PM

Well I don't really want to modify it, I want everything done for me automatically because I'm lazy ;-)  


Perhaps I'm missing something in my understanding as I only looked at the created tables after data migration. Does orchard deal with assigning the value to Id somewhere internal itself when new rows are added? 

If a I take a look at Orchard_Roles_UserRolesPartRecord for example (first one I picked at random) Identity is set to true, Increment 1, and seed 1. My new table has identity set to false

Apr 6, 2011 at 8:26 PM

Do you have the ContentPartRecord bit?

Apr 6, 2011 at 8:31 PM

Do you mean my FormalTitleRecord / PersonPartRecord bit in the db?


if so yup, i do 

Apr 6, 2011 at 8:33 PM

That should be enough for you not to worry at all about ids.

Apr 6, 2011 at 8:44 PM

Ok  so the ids will just magically work themselves out and increment as needed when new people come and go ? If so that's great :D


Actually I am just looking at Orchard_Users_UserPartRecord and see that there is nothing special set on the Id there.


Ok onwards and upwards.... Tally....ho  ;)




PS: good work guys, I really like what I see so far :D