Adding a new table Id column

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Does anyone know Id column is a required field if I want to add my custom table to Nop?

for example,
      AreaCode, Title
       02            Sydney
        03           Melborne ......

But it keeps saying ID column is missing. Areacode is a primary key field.

Thanks

--------------------------------------------------------------------------------

1. in Doamin
namespace Nop.Core.Domain.Common
{
    public class PhoneAreacode : BaseEntity, ICloneable
    {
        public virtual string Areacode { get; set; }
        public virtual string Title { get; set; }
        public object Clone()
        {
            var areacode = new PhoneAreacode()
            {
                Areacode = this.Areacode,
                Title = this.Title,
            };
            return areacode;
        }
    }
}

2. in Data Mapping
namespace Nop.Data.Mapping.Common
{
    class PhoneAreacodeMap : EntityTypeConfiguration<PhoneAreacode>
    {
        public PhoneAreacodeMap()
        {
            this.ToTable("JnJ_PhoneAreacode");
            this.HasKey(a => a.Areacode);
        }
    }
}

3. Service
namespace Nop.Services.Common
{
    public class PhoneAreacodeService: IPhoneAreacodeService
    {
        private readonly IRepository<Nop.Core.Domain.Common.PhoneAreacode> _phoneAreacodeRepository;
        public PhoneAreacodeService(IRepository<Nop.Core.Domain.Common.PhoneAreacode> phoneAreacodeRepository)
        {
            _phoneAreacodeRepository = phoneAreacodeRepository;
        }
        public List<Nop.Core.Domain.Common.PhoneAreacode> GetAllAreacodes()
        {
            return _phoneAreacodeRepository.Table.ToList();
        }
    }
}

namespace Nop.Services.Common
{
    public interface IPhoneAreacodeService
    {
        List<Nop.Core.Domain.Common.PhoneAreacode> GetAllAreacodes();
    }
}
11 years ago
Just put an ID field on the record.

You may also want to consider having a published field, I feel that CreatedOn and UpdatedOn fields are a little overkill for your table however.

HTH

Dave
11 years ago
good idea to have a published field. Thanks. The custom table must have a column Id. Otherwise it won't work.

thanks
11 years ago
I feel it's invariably a good idea to have a record id field on a table, some don't strictly need it, but having seen the sort of horrible things that occur when people don't include id fields, it's probably best if one is included.
11 years ago
Usually Entity is fussy about not having some kind of primary key (id field).  I experienced that a few times when adding tables to my current implementation for Nop.  To save trouble, an id field would be advisable.  I often find myself adding another field that I reference (ProductId for example).  You can make Entity work without the id field but it's troublesome to say the least.
11 years ago
What do you do with an existing table that already has an id, but it's not called 'Id'?  I have the same issue with a custom table called SchoolId, but EF keeps adding 'Id' to the query making it fail.  

I tried the solution described here and it didn't fix the problem.  Is there a way a to pass SchoolModel (which has a [Key] attribute)  to PagedList?

Restructuring the table is not an option because it's shared by another application.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.