c# - DataAccess Plugin in NopCommerce 3.1 -
i trying create plugin data access based on tutorial.
when install
method called in turn calls createdatabaseinstallationscript
. scripts generated plugin in tutorial generate sql
custom tables.
but when function called in plugin, generates sql
30 tables.
following dbcontext
class:
public class myproductobjectcontext : dbcontext, idbcontext { public myproductobjectcontext(string connectionstring) : base(connectionstring){} protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<rewardpointshistory>() .hasrequired(p => p.usedwithorder).withoptional(); modelbuilder.configurations.add(new myproductmap()); modelbuilder.configurations.add(new myproductnotemap()); base.onmodelcreating(modelbuilder); } public string createdatabaseinstallationscript() { return ((iobjectcontextadapter)this).objectcontext .createdatabasescript(); } public void install() { //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<myproductobjectcontext>(null); var scripts = createdatabaseinstallationscript(); database.executesqlcommand(scripts); savechanges(); } public void uninstall() { var dbscript = @"drop table ltmyproduct go drop table ltmyproductnote go "; database.executesqlcommand(dbscript); savechanges(); } public new idbset<tentity> set<tentity>() tentity : baseentity { return base.set<tentity>(); } }
none of entities refer of nop.core.domain
entities. entities code follows:
public class myproduct : baseentity { public int customerid { get; set; } public customer customer { get; set; } public int productid { get; set; } //public virtual product product { get; set; } public string productname { get; set; } public int notescount { get; set; } public virtual icollection<myproductnote> items { get; set; } public myproduct() { items = new hashset<myproductnote>(); } }
and
public class myproductnote : baseentity { public string firstname { get; set; } public string middlename { get; set; } public string lastname { get; set; } public string note { get; set; } public int myproductid { get; set; } public virtual myproduct myproduct { get; set; } public string fullname { { return firstname + (string.isnullorwhitespace(middlename) ? "" : (" " + middlename)) + " " + lastname; } } }
the mapping (entitytypeconfiguration
) both classes below:
public class myproductmap : entitytypeconfiguration<myproduct> { public myproductmap() { totable("ltmyproduct"); haskey(x => x.id); property(x => x.customerid).isrequired(); property(x => x.productid).isrequired(); property(x => x.productname).isunicode() .isvariablelength().hasmaxlength(250).isoptional(); hasmany(x => x.items); } }
and
public class myproductnotemap : entitytypeconfiguration<myproductnote> { public myproductnotemap() { totable("ltmyproductnote"); haskey(x => x.id); property(x => x.firstname).hasmaxlength(50).isrequired() .isunicode().isvariablelength(); property(x => x.middlename).hasmaxlength(50).isunicode() .isvariablelength(); property(x => x.lastname).hasmaxlength(50).isrequired().isunicode() .isvariablelength(); property(x => x.note).isvariablelength().hasmaxlength(1024) .isunicode(); property(x => x.myproductid).isrequired().hascolumntype("int"); } }
i have posted question on nopcommerce forum.
remove line on onmodelcreating method
modelbuilder.entity() .hasrequired(p => p.usedwithorder).withoptional();
this line refrencig core data model
Comments
Post a Comment