ContentItemRecord as a Property in a Different ContentItemRecord

Topics: Customizing Orchard, General, Writing modules
Nov 19, 2012 at 11:51 AM

Consider the case where i need a ProductPartRecord and a ManufacturerPartRecord type.
In the fist type i need a Property of type ManufacturerPartRecord to know the product's manufacturer.
In the second type i would need a IList<ProductPartRecord> property, to keep track of the Products this Manufacturer makes. 

So the question is, should i do it like that, or is it a bad practice to store another part's record in my own part record?

Coordinator
Nov 23, 2012 at 3:34 AM

http://docs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations

Nov 24, 2012 at 11:45 AM
Edited Nov 24, 2012 at 11:47 AM
bertrandleroy wrote:

http://docs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations

Hi bertrandleroy,

I read the link above, in the 1-n relation, we don't have the 'StatePart' class, I want to make the State to be ContentPart so that I can insert/edit/delete in dashboard. So I create the 'StatePart' class derive from ContentPart:

 

    public class StatePart : ContentPart<StateRecord> {
        public string Code {
            get { return Record.Code; }
            set { Record.Code = value; }
        }
        public string Name {
            get { return Record.Name; }
            set { Record.Name = value; }
        }
    }

 

remove the Id property in StateRecird, in Migration.cs remove the Id row and put the .ContentPartRecord() instead:

            SchemaBuilder.CreateTable("StateRecord",
                table => table
                    .ContentPartRecord()
                    .Column("Code", column => column.WithLength(2))
                    .Column("Name")
                );
But it seem doesn't work :(. 

Nov 25, 2012 at 10:47 PM
Edited Nov 25, 2012 at 10:48 PM

Hey jaychen88, you can check this SO thread .

Coordinator
Nov 26, 2012 at 8:36 AM

How does it not work?

Nov 27, 2012 at 11:06 AM
bertrandleroy wrote:

How does it not work?

Hi all,

It throw error at the line of creating dummy 'states':

        private readonly IEnumerable<StateRecord> _states =
            new List<StateRecord> {
                new StateRecord {Code = "AL", Name = "Alabama"},
                new StateRecord {Code = "AK", Name = "Alaska"},
                new StateRecord {Code = "AZ", Name = "Arizona"},
};

        public int UpdateFrom1() {
            if (_stateRepository == null) throw new InvalidOperationException("Couldn't find state repository.");
            foreach (var state in _states) {
                _stateRepository.Create(state);
            }
            return 2;
        }

 

the error is 'attempted to assign id from null one-to-one property: ContentItemRecord'. just remove the creating states part, it will works well.

but still don't know why :(

Coordinator
Nov 27, 2012 at 6:21 PM

Ah, I see. If you want to make states content items, then you can't create them from the repository anymore, as they now need a content item record to be created for each content part record (that's why you're seeing that error). Instead, create the state content items from ContentManager.