Here is how to extend tables using Generic Attributes. I have used it to extend Product as an example.
Generic Attribute Schema:
Column Type
Id int
EntityId int
KeyGroup nvarchar
Key nvarchar
Value nvarchar
Sample Data:
Id EntityId KeyGroup Key Value
1 1 Customer FirstName Hari
2 1 Customer LastName Dupati
32 54845 ProductVariant Barcode 111
33 54845 ProductVariant YearToDateSales 222
Usage:1. Create SystemProductAttributeNames class under Nop.Core.Domain.Catalog Namespace
namespace Nop.Core.Domain.Catalog
{
public partial class SystemProductAttributeNames
{
public static string Barcode { get { return "Barcode"; } }
public static string YearToDateSales { get { return "YearToDateSales"; } }
}
}
2. Inserting or Updating Attributes for an Entity.
_genericAttributeService = EngineContext.Current.Resolve<IGenericAttributeService>();
/// create an instance of productVariant
_genericAttributeService.SaveAttribute(productVariant, SystemProductAttributeNames.Barcode, "111");
_genericAttributeService.SaveAttribute(productVariant, SystemProductAttributeNames.YearToDateSales, "222");
3.Retreiving Attribute value based on Keygroup,Entity and Key using GenericAttributeServiceExtensions
string barcode = productVariant.GetAttribute<string>(SystemProductAttributeNames.Barcode);
decimal yeartodatesales = productVariant.GetAttribute<decimal>(SystemProductAttributeNames.YearToDateSales);
Nop Code Reference:
Nop.Services: Nop.Services\Common\IGenericAttributeService.cs
IGenericAttributeService (in Nop.Services.Common)
• DeleteAttribute(GenericAttribute attribute):void
• GetAttributeById(int attributeId):GenericAttribute
• InsertAttribute(GenericAttribute attribute):void
• UpdateAttribute(GenericAttribute attribute):void
• GetAttributesForEntity(int entityId, string keyGroup):IList
• SaveAttribute(BaseEntity entity, string key, TPropType value):void
GenericAttributeExtentions (in Nop.Services.Common)
• GetAttribute(this BaseEntity entity, string key):TPropType
• GetAttribute(this BaseEntity entity, string key, IGenericAttributeService genericAttributeService):TPropType