Plugin with data access and multi-table

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Do we have any tutorial about writing a plugin with data access and multiple tables?
11 years ago
vanducsonha wrote:
Do we have any tutorial about writing a plugin with data access and multiple tables?


Have you read this? https://www.nopcommerce.com/documentation.aspx.
11 years ago
wooncherk wrote:

Already. But the tutorials is just for one new table (TrackingRecord). I'm wondering how can we create 2 table with 1-n relationship automatically on model creating.

For one table:

modelBuilder.Configurations.Add(new ContentCategoryMap());


But how about multiple tables?
11 years ago
vanducsonha wrote:
Have you read this? https://www.nopcommerce.com/documentation.aspx.

Already. But the tutorials is just for one new table (TrackingRecord). I'm wondering how can we create 2 table with 1-n relationship automatically on model creating.

For one table:

modelBuilder.Configurations.Add(new ContentCategoryMap());


But how about multiple tables?


Just create multiple class like you normally would do, and reference them using Navigation Properties if required. There really isn't any difference whether you are using 1 or multiple tables. :D
11 years ago
1 Customer -> Many Orders


Customer Core.Domain class has a collection of Orders

private ICollection<Order> _orders;


Order Core.Domain class has one Customer

public virtual Customer Customer { get; set; }


The Order's mapping class defines the relationship:

this.HasRequired(o => o.Customer)     //fills the Customer on the Order object
     .WithMany(c => c.Orders)              //fills the Order collection on the Customer object
     .HasForeignKey(o => o.CustomerId);   //tells it what field you use for the relationship
11 years ago
Also, if you need to do a Many-to-Many:

Create the collection on one or both objects:
public virtual ICollection<StateProvince> TaxExemptStates
        {
            get { return _taxExemptStates ?? (_taxExemptStates = new List<StateProvince>()); }
            protected set { _taxExemptStates = value; }
        }


and in the data mapping class:


this.HasMany(c => c.TaxExemptStates)
     .WithMany()    //I left it empty, I don't need an object on each State that has all the customers in it
     .Map(m => m.ToTable("Customer_TaxExemptStates"));


The actual table in the DB should be:
EntityName1_Id
EntityName2_Id

So in my example my Customer_TaxExemptStates table is:

Customer_Id
StateProvince_Id
7 years ago
Should we add in this way?


  modelBuilder.Configurations.Add(new Table1Map());
  modelBuilder.Configurations.Add(new Table2Map());


Interesting things happen after I put these lines in my plugin. It tries to install core nop tables (address, stores etc.) rather than plugin tables.

Still finding way to make it work.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.