a.m. wrote:1. Please clarify. New entity (maybe, property)? No
2. No
3. No
I found a simple way to add new fields or add new entities (that have relations with existing entities) that is not viea plugin but fairly safe future wise:
1) To add a new entity (that has to relate to existing entities) or add new fields to an existing one, add a new folder to Nop.Core.Domain called Extentions. Then under Exentions add a folder with what you are doing name e.g. MyExtention.
2) Add any new Entities that need relate to existing entities here.
3) Also if you need to add new fields to an existing entity, add it here with the same name as the existing entitiy specified using the same original name space. Do this using a 'partial' class (you will note that all nopCommerce classes are marked 'partial' which make them ripe for extending by adding a 2nd partial class to them. Say for example, if you need to a new entity that is based off of Customer. The following file was added to the Nop.Core.Domain.Extentions.Licensing directory but with it's origiinal name space. Because it and the original Customer class is partial, these 2 classes merge into one. (NOTE: this only works if both partial classes are in the same assembly, ie it won't work if you place it in a plugin for example)
using Nop.Core.Domain.Extentions.Licensing;
namespace Nop.Core.Domain.Customers
{
/// <summary>
/// Represents a customer
/// </summary>
public partial class Customer : BaseEntity
{
ICollection<License> _licenses;
public virtual ICollection<License> Licenses
{
get { return _licenses ?? (_licenses = new List<License>()); }
set { _licenses = value; }
}
public virtual void AddLicense(License license)
{
if (!this.Licenses.Contains(license))
{
this.Licenses.Add(license);
license.Customer = this;
}
}
public virtual void RemoveLicense(License license)
{
if (this.Licenses.Contains(license))
{
if (license.Customer == this)
license.Customer = null;
this.Licenses.Remove(license);
}
}
}
}
and then in my case I have a license class
If you add any new entities, then add an Extention directory to Nop.Data.Mapping. Then add your project name e.g. MyExtention to the Extention directory.
Now you add a EntityMap file under that director. In this case a added a LicenseMap.cs class
namespace Nop.Data.Mappings.Extentions.Licensing
{
public partial class LicenseMap : EntityTypeConfiguration<License>
{
public LicenseMap()
{
this.ToTable("License");
.......... removed
this.Property(L => L.Serial).HasColumnType("int");
this.Property(L => L.Capabilities).HasColumnType("int");
// Many to us foreign keys
// Customer
this.HasOptional(L => L.Customer)
.WithMany(C => C.Licenses)
.HasForeignKey(L => L.CustomerId);
// Product Variant
this.HasRequired(L => L.ProductVariant)
.WithMany()
.HasForeignKey(L => L.ProductVariantId);
.... removed }
}
}
So now a customer is related to a license in that a customer can have 0 or more licenses. (Although I didnt show it, it also has a required relationship of a productvariant).
The only that won't easily work using this extention method is that if you want to add a new filed that requires fluent api to an existing entity because you can't extent the 'mapping' classes. But if you notice that even though I created a relationship between Customer and License, the mapping is done in hte LicenseMap class referencing the collection in customer so I didn't need to change the customermap.cs file.
Using this way to extend creates all your extention code (to existing nopcommere code) into a separate folder. And if you create a subfolder under extention for you project, it will less likely conflict with someone elses future extention.
So the anwer you got is correct, but here is a way to modify the existing code base and minimize stepping all over it (at least with entities that need relations with existing entities)
Bernie