nopcommerce - How should I add new table from plugin -


i have made new plugin name nop.plugin.mostviewproduct.product , want know how should insert new table plugin's model file?

model file path :

nopcommerce_3.20_source\plugins\nop.plugin.mostviewproduct.product\models 

please advise!

:) .. if created new entity plugin plugin must install , update database let plugin project .. have : 1- create entity in domain folder (for example) 2- create entitymap in data folder (for example) 3- create pluginobjectcontext in data folder (for example) 4- create efstartuptask in data folder (for example)

as example:

1- create entity

public partial class mostviewedproduct : baseentity {     /// <summary>     /// gets or sets name     /// </summary>     public string name { get; set; } } 

2- create 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>() tentity : baseentity     {         return base.set<tentity>();     }      /// <summary>     /// install     /// </summary>     public void install()     {         //create table         var dbscript = createdatabasescript();         database.executesqlcommand(dbscript);         savechanges();     }      /// <summary>     /// uninstall     /// </summary>     public void uninstall()     {         //drop table         this.dropplugintable("mostviewedproduct");     }      /// <summary>     /// execute stores procedure , load list of entities @ 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) tentity : baseentity, new()     {         throw new notimplementedexception();     }      /// <summary>     /// creates raw sql query return elements of given generic type.  type can type has properties match names of columns returned query, or can simple primitive type. type not have entity type. results of query never tracked context if type of object returned entity type.     /// </summary>     /// <typeparam name="telement">the type of object returned query.</typeparam>     /// <param name="sql">the sql query string.</param>     /// <param name="parameters">the parameters apply sql query string.</param>     /// <returns>result</returns>     public ienumerable<telement> sqlquery<telement>(string sql, params object[] parameters)     {         throw new notimplementedexception();     }      /// <summary>     /// executes given ddl/dml command against database.     /// </summary>     /// <param name="sql">the command string</param>     /// <param name="donotensuretransaction">false - transaction creation not ensured; true - transaction creation ensured.</param>     /// <param name="timeout">timeout value, in seconds. null value indicates default value of underlying provider used</param>     /// <param name="parameters">the parameters apply command string.</param>     /// <returns>the result returned database after executing command.</returns>     public int executesqlcommand(string sql, bool donotensuretransaction = false, int? timeout = null, params object[] parameters)     {         throw new notimplementedexception();     } } 

4- create efstartuptask

public class efstartuptask : istartuptask {     public void execute()     {         //it's required set initializer null (for sql server compact).         //otherwise, you'll "the model backing 'your context name' context has changed since database created. consider using code first migrations update database"         database.setinitializer<mostviewedproductobjectcontext>(null);     }      public int order     {         { return 0; }     } } 

this how things works install plugin , uninstall itself


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -