我已将新的插件名称设为Nop.Plugin.MostViewProduct.Product
,我想知道如何从插件的模型文件中插入新表?
模型文件路径为:
nopCommerce_3.20_Source\Plugins\Nop.Plugin.MostViewProduct.Product\Models
请指教!
答案 0 :(得分:3)
:) ..如果您从插件中创建了一个新实体,以便插件必须安装并更新数据库,以便让插件项目执行此操作...您必须: 1-在域文件夹中创建实体(例如) 2-在数据文件夹中创建EntityMap(例如) 3-在数据文件夹中创建PluginObjectContext(例如) 4-在数据文件夹中创建EfStartUpTask(例如)
作为一个例子:
1-创建实体
public partial class MostViewedProduct : BaseEntity
{
/// <summary>
/// Gets or sets the name
/// </summary>
public string Name { get; set; }
}
2-创建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-创建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-创建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; }
}
}
这就是从自身安装插件并自行卸载的方法