Question

I have made a new plugin name as Nop.Plugin.MostViewProduct.Product and I want to know that how should I insert new table from plugin's model file?

Model file path is :

nopCommerce_3.20_Source\Plugins\Nop.Plugin.MostViewProduct.Product\Models

Please advise!

Was it helpful?

Solution

:) .. If you created a new entity from your plugin so that the plugin must install and update the database, so to let the plugin project do that ..

You have to :

  1. Create the Entity in Domain Folder (for example)
  2. Create a EntityMap in the Data Folder (for example)
  3. Create PluginObjectContext in Data Folder (for example)
  4. Create an EfStartUpTask in Data Folder (for example)

as an example:

1- Create the Entity

public partial class MostViewedProduct : BaseEntity
{
    /// <summary>
    /// Gets or sets the name
    /// </summary>
    public string Name { get; set; }
}

2- Create a EntityMap

public partial class MostViewedProductMap : EntityTypeConfiguration<MostViewedProduct>
{
    public MostViewedProductMap()
    {
        this.ToTable("MostViewedProduct");
        this.HasKey(d => d.Id);
        this.Property(d => d.Name).IsRequired().HasMaxLength(400);
    }
}

3- Create PluginObjectContext

public class MostViewedProductObjectContext : DbContext, IDbContext
{
    public MostViewedProductObjectContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
        //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new MostViewedProductMap());

        //disable EdmMetadata generation
        //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
        base.OnModelCreating(modelBuilder);
    }

    public string CreateDatabaseScript()
    {
        return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
    }

    public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
    {
        return base.Set<TEntity>();
    }

    /// <summary>
    /// Install
    /// </summary>
    public void Install()
    {
        //create the table
        var dbScript = CreateDatabaseScript();
        Database.ExecuteSqlCommand(dbScript);
        SaveChanges();
    }

    /// <summary>
    /// Uninstall
    /// </summary>
    public void Uninstall()
    {
        //drop the table
        this.DropPluginTable("MostViewedProduct");
    }

    /// <summary>
    /// Execute stores procedure and load a list of entities at the end
    /// </summary>
    /// <typeparam name="TEntity">Entity type</typeparam>
    /// <param name="commandText">Command text</param>
    /// <param name="parameters">Parameters</param>
    /// <returns>Entities</returns>
    public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Creates a raw SQL query that will return elements of the given generic type.  The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type.
    /// </summary>
    /// <typeparam name="TElement">The type of object returned by the query.</typeparam>
    /// <param name="sql">The SQL query string.</param>
    /// <param name="parameters">The parameters to apply to the SQL query string.</param>
    /// <returns>Result</returns>
    public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Executes the given DDL/DML command against the database.
    /// </summary>
    /// <param name="sql">The command string</param>
    /// <param name="doNotEnsureTransaction">false - the transaction creation is not ensured; true - the transaction creation is ensured.</param>
    /// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
    /// <param name="parameters">The parameters to apply to the command string.</param>
    /// <returns>The result returned by the database after executing the command.</returns>
    public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
    {
        throw new NotImplementedException();
    }
}

4- Create an EfStartUpTask

public class EfStartUpTask : IStartupTask
{
    public void Execute()
    {
        //It's required to set initializer to null (for SQL Server Compact).
        //otherwise, you'll get something like "The model backing the 'your context name' context has changed since the database was created. Consider using Code First Migrations to update the database"
        Database.SetInitializer<MostViewedProductObjectContext>(null);
    }

    public int Order
    {
        get { return 0; }
    }
}

this is how things works to install your plugin from itself and uninstall itself

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top