Pass Foreign key in builder / Load Foreign entity

10 个月 前
Hello,

i´m try to use similliar process to entry foreign entity in my 4.60 plugin like in Linq/Entity framework >> "timeRecord.Tag.ValueForExample" where "Tag" is foreign table.
Foreign entity "Tag" is null everytime.


namespace Nop.Plugin.NCH.Domain.TimeRecord
{
    public class TimeRecord : BaseEntity
    {
        public new Int64 Id { get; set; }
        public Int64 TagId { get; set; }
        public decimal Rssi { get; set; }

        virtual public Tag Tag { get; set; }
    }
}

// builder

namespace Nop.Plugin.NCH.Domain.TimeRecord.Mapping
{
    public class TimeRecordBuilder : NopEntityBuilder<TimeRecord>
    {
        public override void MapEntity(CreateTableExpressionBuilder table)
        {
            table.WithColumn(nameof(TimeRecord.Id)).AsInt64().PrimaryKey()
            .WithColumn(nameof(TimeRecord.TagId)).AsInt64().ForeignKey<Tag>("Tag","Id")
        }
    }
}


I hope is not only possible way load Tag by Id through _tagService and fill entity TimeRecord :-)

Thank you for help.
10 个月 前
Try this
table
    .WithColumn(nameof(TimeRecord.TagId)).AsInt32().ForeignKey<Tag>();


- I don't think you need to specify PrimaryKey, since nopC will take care of that for you.
- I used AsInt32 (not 64), since that is the way all the other nopC tables are defined, but change it if needed.
10 个月 前
Not better. Still null inside :/
10 个月 前
Please explain "Still null".
You still need to create the entities and "save".  The FK is a constraint; it does not create anything, just ensures reference is valid.