How to add 1-N relationship on existing model ?

Topics: Administration, General, Writing modules
Sep 9, 2013 at 9:37 AM
Hi all ,
I already have State Model in my database.
 public class StatePart : ContentPart<StatePartRecord> {
        public string Name{
            get { return Record.Name; }
            set { Record.Name= value; }
        }
        public string Code{
            get { return Record.Code; }
            set { Record.Code= value; }
        }
     
    }
Now i want to create an address model to have 1-N relationship for this model ,
How can i do it ? i try to have address
  public class AddressPart : ContentPart<AddressPartRecord> {
        public string Address {
            get { return Record.Address; }
            set { Record.Address = value; }
        }
        public string City {
            get { return Record.City; }
            set { Record.City = value; }
        }
        public StatePartRecord State {
            get { return Record.StatePartRecord; }
            set { Record.StatePartRecord= value; }
        }
and migration with
SchemaBuilder.CreateTable("AddressPartRecord",
        table => table
            .ContentPartRecord()
            .Column<string>("Address")
            .Column<string>("City")
            .Column<int>("StatePartRecord_Id")
            .Column<string>("Zip")
        );
When i look at the database, the address table wont ref StatePartRecord_Id as foreign key .

Do i need to recreate the db with the relationship as ref http://docs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations ?
or have better way to link my existing model to 1-N relationship ?
Sep 9, 2013 at 3:24 PM
did u put the second code inside
public int UpdateFrom1()
Sep 9, 2013 at 3:33 PM
my migration file
public int Create() {
            // Creating table StatePartRecord
            SchemaBuilder.CreateTable("StatePartRecord", table => table
                .ContentPartRecord()
                .Column("Name", DbType.String)
                .Column("Code", DbType.String)
            );

             ContentDefinitionManager.AlterPartDefinition(
                typeof(StatePartRecord).Name, cfg => cfg.Attachable());


            return 1;
        }

public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterTypeDefinition("StateRecord",
                cfg => cfg
                   .WithPart("StatePartRecord")
                    .WithPart("CommonPart")
                    );

            return2;
        }

public int UpdateFrom2()
        {
            SchemaBuilder.CreateTable("AddressPartRecord",
        table => table
            .ContentPartRecord()
            .Column<string>("Address")
            .Column<string>("City")
            .Column<int>("StatePartRecord_Id")
            .Column<string>("Zip")
        );

            return3;
        }
Coordinator
Sep 9, 2013 at 7:14 PM
Well, you didn't create a foreign key, so of course there isn't one... Also, this looks weird: AlterTypeDefinition("StateRecord". You content type is named "StateRecord"? That's confusing and probably wrong. Why is state a content type in the first place?
Also, what exactly are you trying to do and how does it not work?
Sep 10, 2013 at 12:34 AM
How to i create the foreign key in a existing model ? cause i read the example, must define the primary key in the 1st place before create the model in database, but now i already create the table and the id already generated .
Actually at 1st place this state model are use in a registration system, it only need to display all the input state. but after using for a while , now need to add in every state branches address, so this situation occur.
Coordinator
Sep 10, 2013 at 2:24 AM
What situation? I still don't know what you're trying to do or how it doesn't work. You don't need to create foreign keys, but if you want to, you can do so using the SchemaBuilder, from a migration step.
Sep 10, 2013 at 6:15 AM
Ok , thanks , find the solution. can use the CreateForeignKey method.
New learning MVC , nhibernate with orchard cms, but get great response from the forum.
Like it very much.
Thanks BertrandLeRoy