Migration

1 month ago
I am using nop commerce 4.5 and at the time.of table creation it is showing error in void up method can anyone explain how to create table in nop 4.5
1 month ago
Can you be more specific about your error ? You can try putting your code inside a try catch block Up() method and log the error to find the specific issue.

And to create tables, you need to define your domain/entity first as a public class.
Ex:

public class Address : BaseEntity
{
       public string Location { get; set; }
}


Then define your migration to create the table for you:


[NopMigration("2022/07/07 09:09:17:6455442", "Test.Plugin base schema", MigrationProcessType.Installation)]
    public class SchemaMigration : AutoReversingMigration
    {
        public override void Up()
        {
            Create.TableFor<Address >();
        }
    }


Build the plugin, install it and you should have your table created after installation
1 month ago
Inside of the "domain" namespace we're going to create a public class named ProductViewTrackerRecord. This class extends BaseEntity, but it is otherwise a very simple file. Something to remember is that we do not have navigation properties (relational properties), because the Linq2DB framework, which does not support the navigation properties.

namespace Nop.Plugin.Misc.ProductViewTracker.Domain
{
    public class ProductViewTrackerRecord : BaseEntity
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public int CustomerId { get; set; }
        public string IpAddress { get; set; }
        public bool IsRegistered { get; set; }
    }
}


The next class to create is the FluentMigrator entity builder class. Inside the mapping class, we map the columns, table relationships, and the database table.

using FluentMigrator.Builders.Create.Table;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Data.Mapping.Builders;
using Nop.Plugin.Other.ProductViewTracker.Domains;
using Nop.Data.Extensions;
using System.Data;

namespace Nop.Plugin.Other.ProductViewTracker.Mapping.Builders
{
    public class ProductViewTrackerRecordBuilder : NopEntityBuilder<ProductViewTrackerRecord>
    {
        /// <summary>
        /// Apply entity configuration
        /// </summary>
        /// <param name="table">Create table expression builder</param>
        public override void MapEntity(CreateTableExpressionBuilder table)
        {
            //map the primary key (not necessary if it is Id field)
            table.WithColumn(nameof(ProductViewTrackerRecord.Id)).AsInt32().PrimaryKey()
            //map the additional properties as foreign keys
            .WithColumn(nameof(ProductViewTrackerRecord.ProductId)).AsInt32().ForeignKey<Product>(onDelete: Rule.Cascade)
            .WithColumn(nameof(ProductViewTrackerRecord.CustomerId)).AsInt32().ForeignKey<Customer>(onDelete: Rule.Cascade)
            //avoiding truncation/failure
            //so we set the same max length used in the product name
            .WithColumn(nameof(ProductViewTrackerRecord.ProductName)).AsString(400)
            //not necessary if we don't specify any rules
            .WithColumn(nameof(ProductViewTrackerRecord.IpAddress)).AsString()
            .WithColumn(nameof(ProductViewTrackerRecord.IsRegistered)).AsInt32();
        }
    }
}


The next important class for us will be the migration class, which creates our table directly in the database. You can create as many migrations as you like, the only thing you need to keep track of is the version of your migration. Nopcommerce specially created NopMigration attribute to make it easier for us. By indicating here the most complete and accurate file creation date, you practically guarantee the uniqueness of your migration number.

using FluentMigrator;
using Nop.Data.Extensions;
using Nop.Data.Migrations;
using Nop.Plugin.Other.ProductViewTracker.Domains;

namespace Nop.Plugin.Other.ProductViewTracker.Migrations
{
    [NopMigration("2020/05/27 08:40:55:1687541", "Other.ProductViewTracker base schema", MigrationProcessType.Installation)]
    public class SchemaMigration : AutoReversingMigration
    {
        public override void Up()
        {
            Create.TableFor<ProductViewTrackerRecord>();            
        }
    }
}


Reference: https://docs.nopcommerce.com/en/developer/plugins/plugin-with-data-access.html#the-data-access-layer-aka-creating-new-entities-in-nopcommerce